diff --git a/migrations/20200521014822_add_base_language_to_categories.php b/migrations/20200521014822_add_base_language_to_categories.php new file mode 100644 index 0000000000..93f4a20e2e --- /dev/null +++ b/migrations/20200521014822_add_base_language_to_categories.php @@ -0,0 +1,25 @@ +fetchRow( + "SELECT config_value FROM config WHERE group_name='site' and config_key='language' " + ); + // get two letter lang code + $language = str_before(json_decode($result['config_value']), '-'); + $this->table('tags') + ->addColumn('base_language', 'string', ['null' => false, 'default' => $language]) //es/en + ->update(); + } + + + public function down() + { + $this->table('tags')->removeColumn('base_language'); + } +} diff --git a/tests/integration/v4/tags.v4.feature b/tests/integration/v4/tags.v4.feature index 3f3ad49475..c457b86121 100644 --- a/tests/integration/v4/tags.v4.feature +++ b/tests/integration/v4/tags.v4.feature @@ -1,6 +1,6 @@ @tagsFixture @rolesEnabled Feature: Testing the Categories API - Scenario: Creating a new Tag + Scenario: Creating a new Tag with a base language Given that I want to make a new "Category" And that the oauth token is "testadminuser" And that the api_url is "api/v4" @@ -14,6 +14,7 @@ Feature: Testing the Categories API "type":"category", "priority":1, "color":"00ff00", + "base_language": "en", "role": ["admin", "user"] } """ @@ -27,10 +28,47 @@ Feature: Testing the Categories API And the "result.color" property equals "#00ff00" And the "result.priority" property equals "1" And the "result.type" property equals "category" + And the "result.enabled_languages.default" property equals "en" + And the response has a "result.role" property + And the "result.parent.id" property equals "1" + Then the guzzle status code should be 201 + Scenario: Creating a new Tag with a base language and translation + Given that I want to make a new "Category" + And that the oauth token is "testadminuser" + And that the api_url is "api/v4" + And that the request "data" is: + """ + { + "parent_id":1, + "tag":"Boxes with a translation", + "description":"Is this a box? Awesome", + "type":"category", + "priority":1, + "color":"00ff00", + "base_language": "en", + "role": ["admin", "user"], + "translations": { + "es": { + "tag": "Cajas" + } + } + } + """ + When I request "/categories" + Then the response is JSON + And the response has a "result.id" property + And the type of the "result.id" property is "numeric" + And the "result.tag" property equals "Boxes with a translation" + And the "result.slug" property equals "boxes-with-a-translation" + And the "result.description" property equals "Is this a box? Awesome" + And the "result.color" property equals "#00ff00" + And the "result.priority" property equals "1" + And the "result.type" property equals "category" + And the "result.enabled_languages.default" property equals "en" + And the "result.enabled_languages.available.0" property equals "es" And the response has a "result.role" property And the "result.parent.id" property equals "1" Then the guzzle status code should be 201 - Scenario: Creating a duplicate tag Given that I want to make a new "Category" And that the oauth token is "testadminuser" diff --git a/v4/Http/Resources/CategoryResource.php b/v4/Http/Resources/CategoryResource.php index c0ad46df7f..c6db9a4743 100644 --- a/v4/Http/Resources/CategoryResource.php +++ b/v4/Http/Resources/CategoryResource.php @@ -29,6 +29,10 @@ public function toArray($request) 'children' => $this->children, 'parent' => $this->parent, 'translations' => new TranslationCollection($this->translations), + 'enabled_languages' => [ + 'default'=> $this->base_language, + 'available' => $this->translations->groupBy('language')->keys() + ] ]; } } diff --git a/v4/Models/Category.php b/v4/Models/Category.php index ece200e88a..45e1d80c41 100644 --- a/v4/Models/Category.php +++ b/v4/Models/Category.php @@ -58,7 +58,8 @@ class Category extends Model 'icon', 'description', 'role', - 'priority' + 'priority', + 'base_language' ]; /**