From 4674631114063e1b65970da7a473f41560b79d08 Mon Sep 17 00:00:00 2001 From: jkindly Date: Thu, 1 Aug 2024 11:11:59 +0200 Subject: [PATCH 1/7] OP-463: Block templates --- src/Form/Type/BlockType.php | 4 ++++ .../TemplateBlockAutocompleteChoiceType.php | 19 +++++++++++++++++ src/Form/Type/TemplateType.php | 1 + .../assets/admin/js/bitbag/bitbag-template.js | 14 ++++++------- .../config/routing/admin/template.yml | 21 ++++++++++++++++--- .../views/Block/Crud/_form.html.twig | 2 ++ src/Resources/views/Form/theme.html.twig | 6 +++++- src/Resources/views/Page/Crud/_form.html.twig | 14 +------------ src/Resources/views/Template/form.html.twig | 13 ++++++++++++ 9 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 src/Form/Type/TemplateBlockAutocompleteChoiceType.php create mode 100644 src/Resources/views/Template/form.html.twig diff --git a/src/Form/Type/BlockType.php b/src/Form/Type/BlockType.php index d2472dc3..6713a45c 100755 --- a/src/Form/Type/BlockType.php +++ b/src/Form/Type/BlockType.php @@ -72,6 +72,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'multiple' => true, 'help' => 'bitbag_sylius_cms_plugin.ui.display_for_taxons.help', ]) + ->add('template', TemplateBlockAutocompleteChoiceType::class, [ + 'label' => false, + 'mapped' => false, + ]) ; } diff --git a/src/Form/Type/TemplateBlockAutocompleteChoiceType.php b/src/Form/Type/TemplateBlockAutocompleteChoiceType.php new file mode 100644 index 00000000..c0737319 --- /dev/null +++ b/src/Form/Type/TemplateBlockAutocompleteChoiceType.php @@ -0,0 +1,19 @@ + 'bitbag_sylius_cms_plugin.ui.type', 'choices' => [ 'bitbag_sylius_cms_plugin.ui.page' => 'page', + 'bitbag_sylius_cms_plugin.ui.block' => 'block', ], ]) ->add('contentElements', CollectionType::class, [ diff --git a/src/Resources/assets/admin/js/bitbag/bitbag-template.js b/src/Resources/assets/admin/js/bitbag/bitbag-template.js index aeea6ac5..6ca7984b 100644 --- a/src/Resources/assets/admin/js/bitbag/bitbag-template.js +++ b/src/Resources/assets/admin/js/bitbag/bitbag-template.js @@ -9,19 +9,20 @@ export class HandleTemplate { $(document).ready(() => { const cmsLoadTemplate = $('[data-bb-cms-load-template]'); const cmsPageTemplate = $('#bitbag_sylius_cms_plugin_page_template'); + const cmsBlockTemplate = $('#bitbag_sylius_cms_plugin_block_template'); cmsLoadTemplate.on('click', function (e) { e.preventDefault(); - if (!cmsPageTemplate.val()) { + if (!cmsPageTemplate.val() && !cmsBlockTemplate.val()) { return; } $('#load-template-confirmation-modal').modal('show'); }); - $('#load-template-confirmation-button').on('click', function (e) { - const templateId = cmsPageTemplate.val(); + $('#load-template-confirmation-button').on('click', function () { + const templateId = cmsPageTemplate.val() ?? cmsBlockTemplate.val(); if (!templateId) { return; } @@ -34,9 +35,6 @@ export class HandleTemplate { $.ajax({ url: endpointUrl, type: 'GET', - headers: { - 'X-Requested-With': 'XMLHttpRequest' - }, success: function(data) { if (data.status === 'success') { $('[id^="bitbag_sylius_cms_plugin_"][id$="contentElements"]') @@ -47,7 +45,9 @@ export class HandleTemplate { $('[data-form-collection="add"]').trigger('click'); }); - const elements = $('[id^="bitbag_sylius_cms_plugin_page_contentElements_"][id$="_type"]'); + const elements = $('[id^="bitbag_sylius_cms_plugin_"][id*="_contentElements_"][id$="_type"]').filter(function() { + return /_page_|_block_/.test(this.id); + }); $.each(data.content, function (index, element) { elements.eq(index).val(element.type); diff --git a/src/Resources/config/routing/admin/template.yml b/src/Resources/config/routing/admin/template.yml index 129cd1bb..9013de97 100755 --- a/src/Resources/config/routing/admin/template.yml +++ b/src/Resources/config/routing/admin/template.yml @@ -16,7 +16,7 @@ bitbag_sylius_cms_plugin_admin_template: type: sylius.resource bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase: - path: /ajax/templates/search + path: /ajax/templates/page/search methods: [GET] defaults: _format: json @@ -30,7 +30,22 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase: phrase: $phrase type: page -bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id: +bitbag_sylius_cms_plugin_admin_ajax_template_block_by_name_phrase: + path: /ajax/templates/block/search + methods: [GET] + defaults: + _format: json + _controller: bitbag_sylius_cms_plugin.controller.template::indexAction + _sylius: + serialization_groups: [Autocomplete] + permission: true + repository: + method: findTemplatesByNamePart + arguments: + phrase: $phrase + type: block + +bitbag_sylius_cms_plugin_admin_ajax_template_by_id: path: /ajax/templates/id methods: [GET] defaults: @@ -43,7 +58,7 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id: method: find arguments: [id: $id] -bitbag_sylius_cms_plugin_admin_ajax_template_page_content_by_id: +bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id: path: /ajax/templates/content/{id} methods: [GET] defaults: diff --git a/src/Resources/views/Block/Crud/_form.html.twig b/src/Resources/views/Block/Crud/_form.html.twig index d53d42d7..e2905386 100755 --- a/src/Resources/views/Block/Crud/_form.html.twig +++ b/src/Resources/views/Block/Crud/_form.html.twig @@ -1,5 +1,6 @@ {% from '@BitBagSyliusCmsPlugin/Macro/translationForm.html.twig' import translationForm %} {% form_theme form '@BitBagSyliusCmsPlugin/Form/theme.html.twig' %} +{% include '@BitBagSyliusCmsPlugin/Modal/_loadTemplateConfirmation.html.twig' %}
@@ -22,6 +23,7 @@

{{ 'bitbag_sylius_cms_plugin.ui.content_elements.title'|trans }}

+ {% include '@BitBagSyliusCmsPlugin/Template/form.html.twig' with {ajax_url: path('bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id', {'id': 'REPLACE_ID'}) } %}
{{ form_row(form.contentElements) }} diff --git a/src/Resources/views/Form/theme.html.twig b/src/Resources/views/Form/theme.html.twig index 4f31f175..1a33ad39 100755 --- a/src/Resources/views/Form/theme.html.twig +++ b/src/Resources/views/Form/theme.html.twig @@ -13,7 +13,11 @@ {% endblock %} {% block bitbag_template_page_autocomplete_choice_row %} - {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id')}) }} + {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_by_id')}) }} +{% endblock %} + +{% block bitbag_template_block_autocomplete_choice_row %} + {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_block_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_by_id')}) }} {% endblock %} {% block bitbag_media_autocomplete_choice_row %} diff --git a/src/Resources/views/Page/Crud/_form.html.twig b/src/Resources/views/Page/Crud/_form.html.twig index cc75f04c..97fb6a55 100755 --- a/src/Resources/views/Page/Crud/_form.html.twig +++ b/src/Resources/views/Page/Crud/_form.html.twig @@ -24,19 +24,7 @@

{{ 'bitbag_sylius_cms_plugin.ui.content_elements.title'|trans }}

-
-
{{ 'bitbag_sylius_cms_plugin.ui.use_page_template'|trans }}
-
-
- {{ form_row(form.template) }} -
- -
-
+ {% include '@BitBagSyliusCmsPlugin/Template/form.html.twig' with {ajax_url: path('bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id', {'id': 'REPLACE_ID'}) } %}
{{ form_row(form.contentElements) }} diff --git a/src/Resources/views/Template/form.html.twig b/src/Resources/views/Template/form.html.twig new file mode 100644 index 00000000..c53272e2 --- /dev/null +++ b/src/Resources/views/Template/form.html.twig @@ -0,0 +1,13 @@ +
+
{{ 'bitbag_sylius_cms_plugin.ui.use_page_template'|trans }}
+
+
+ {{ form_row(form.template) }} +
+ +
+
From b63950f84c52e1a9002b962848e2f698a0abd800 Mon Sep 17 00:00:00 2001 From: jkindly Date: Thu, 1 Aug 2024 11:11:59 +0200 Subject: [PATCH 2/7] OP-463: Block templates --- src/Form/Type/BlockType.php | 4 ++++ .../TemplateBlockAutocompleteChoiceType.php | 19 +++++++++++++++++ src/Form/Type/TemplateType.php | 1 + .../assets/admin/js/bitbag/bitbag-template.js | 14 ++++++------- .../config/routing/admin/template.yml | 21 ++++++++++++++++--- .../views/Block/Crud/_form.html.twig | 2 ++ src/Resources/views/Form/theme.html.twig | 6 +++++- src/Resources/views/Page/Crud/_form.html.twig | 14 +------------ src/Resources/views/Template/form.html.twig | 13 ++++++++++++ 9 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 src/Form/Type/TemplateBlockAutocompleteChoiceType.php create mode 100644 src/Resources/views/Template/form.html.twig diff --git a/src/Form/Type/BlockType.php b/src/Form/Type/BlockType.php index d2472dc3..6713a45c 100755 --- a/src/Form/Type/BlockType.php +++ b/src/Form/Type/BlockType.php @@ -72,6 +72,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'multiple' => true, 'help' => 'bitbag_sylius_cms_plugin.ui.display_for_taxons.help', ]) + ->add('template', TemplateBlockAutocompleteChoiceType::class, [ + 'label' => false, + 'mapped' => false, + ]) ; } diff --git a/src/Form/Type/TemplateBlockAutocompleteChoiceType.php b/src/Form/Type/TemplateBlockAutocompleteChoiceType.php new file mode 100644 index 00000000..c0737319 --- /dev/null +++ b/src/Form/Type/TemplateBlockAutocompleteChoiceType.php @@ -0,0 +1,19 @@ + 'bitbag_sylius_cms_plugin.ui.type', 'choices' => [ 'bitbag_sylius_cms_plugin.ui.page' => 'page', + 'bitbag_sylius_cms_plugin.ui.block' => 'block', ], ]) ->add('contentElements', CollectionType::class, [ diff --git a/src/Resources/assets/admin/js/bitbag/bitbag-template.js b/src/Resources/assets/admin/js/bitbag/bitbag-template.js index aeea6ac5..6ca7984b 100644 --- a/src/Resources/assets/admin/js/bitbag/bitbag-template.js +++ b/src/Resources/assets/admin/js/bitbag/bitbag-template.js @@ -9,19 +9,20 @@ export class HandleTemplate { $(document).ready(() => { const cmsLoadTemplate = $('[data-bb-cms-load-template]'); const cmsPageTemplate = $('#bitbag_sylius_cms_plugin_page_template'); + const cmsBlockTemplate = $('#bitbag_sylius_cms_plugin_block_template'); cmsLoadTemplate.on('click', function (e) { e.preventDefault(); - if (!cmsPageTemplate.val()) { + if (!cmsPageTemplate.val() && !cmsBlockTemplate.val()) { return; } $('#load-template-confirmation-modal').modal('show'); }); - $('#load-template-confirmation-button').on('click', function (e) { - const templateId = cmsPageTemplate.val(); + $('#load-template-confirmation-button').on('click', function () { + const templateId = cmsPageTemplate.val() ?? cmsBlockTemplate.val(); if (!templateId) { return; } @@ -34,9 +35,6 @@ export class HandleTemplate { $.ajax({ url: endpointUrl, type: 'GET', - headers: { - 'X-Requested-With': 'XMLHttpRequest' - }, success: function(data) { if (data.status === 'success') { $('[id^="bitbag_sylius_cms_plugin_"][id$="contentElements"]') @@ -47,7 +45,9 @@ export class HandleTemplate { $('[data-form-collection="add"]').trigger('click'); }); - const elements = $('[id^="bitbag_sylius_cms_plugin_page_contentElements_"][id$="_type"]'); + const elements = $('[id^="bitbag_sylius_cms_plugin_"][id*="_contentElements_"][id$="_type"]').filter(function() { + return /_page_|_block_/.test(this.id); + }); $.each(data.content, function (index, element) { elements.eq(index).val(element.type); diff --git a/src/Resources/config/routing/admin/template.yml b/src/Resources/config/routing/admin/template.yml index 129cd1bb..9013de97 100755 --- a/src/Resources/config/routing/admin/template.yml +++ b/src/Resources/config/routing/admin/template.yml @@ -16,7 +16,7 @@ bitbag_sylius_cms_plugin_admin_template: type: sylius.resource bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase: - path: /ajax/templates/search + path: /ajax/templates/page/search methods: [GET] defaults: _format: json @@ -30,7 +30,22 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase: phrase: $phrase type: page -bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id: +bitbag_sylius_cms_plugin_admin_ajax_template_block_by_name_phrase: + path: /ajax/templates/block/search + methods: [GET] + defaults: + _format: json + _controller: bitbag_sylius_cms_plugin.controller.template::indexAction + _sylius: + serialization_groups: [Autocomplete] + permission: true + repository: + method: findTemplatesByNamePart + arguments: + phrase: $phrase + type: block + +bitbag_sylius_cms_plugin_admin_ajax_template_by_id: path: /ajax/templates/id methods: [GET] defaults: @@ -43,7 +58,7 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id: method: find arguments: [id: $id] -bitbag_sylius_cms_plugin_admin_ajax_template_page_content_by_id: +bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id: path: /ajax/templates/content/{id} methods: [GET] defaults: diff --git a/src/Resources/views/Block/Crud/_form.html.twig b/src/Resources/views/Block/Crud/_form.html.twig index d53d42d7..e2905386 100755 --- a/src/Resources/views/Block/Crud/_form.html.twig +++ b/src/Resources/views/Block/Crud/_form.html.twig @@ -1,5 +1,6 @@ {% from '@BitBagSyliusCmsPlugin/Macro/translationForm.html.twig' import translationForm %} {% form_theme form '@BitBagSyliusCmsPlugin/Form/theme.html.twig' %} +{% include '@BitBagSyliusCmsPlugin/Modal/_loadTemplateConfirmation.html.twig' %}
@@ -22,6 +23,7 @@

{{ 'bitbag_sylius_cms_plugin.ui.content_elements.title'|trans }}

+ {% include '@BitBagSyliusCmsPlugin/Template/form.html.twig' with {ajax_url: path('bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id', {'id': 'REPLACE_ID'}) } %}
{{ form_row(form.contentElements) }} diff --git a/src/Resources/views/Form/theme.html.twig b/src/Resources/views/Form/theme.html.twig index 4f31f175..1a33ad39 100755 --- a/src/Resources/views/Form/theme.html.twig +++ b/src/Resources/views/Form/theme.html.twig @@ -13,7 +13,11 @@ {% endblock %} {% block bitbag_template_page_autocomplete_choice_row %} - {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id')}) }} + {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_by_id')}) }} +{% endblock %} + +{% block bitbag_template_block_autocomplete_choice_row %} + {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_block_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_by_id')}) }} {% endblock %} {% block bitbag_media_autocomplete_choice_row %} diff --git a/src/Resources/views/Page/Crud/_form.html.twig b/src/Resources/views/Page/Crud/_form.html.twig index cc75f04c..97fb6a55 100755 --- a/src/Resources/views/Page/Crud/_form.html.twig +++ b/src/Resources/views/Page/Crud/_form.html.twig @@ -24,19 +24,7 @@

{{ 'bitbag_sylius_cms_plugin.ui.content_elements.title'|trans }}

-
-
{{ 'bitbag_sylius_cms_plugin.ui.use_page_template'|trans }}
-
-
- {{ form_row(form.template) }} -
- -
-
+ {% include '@BitBagSyliusCmsPlugin/Template/form.html.twig' with {ajax_url: path('bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id', {'id': 'REPLACE_ID'}) } %}
{{ form_row(form.contentElements) }} diff --git a/src/Resources/views/Template/form.html.twig b/src/Resources/views/Template/form.html.twig new file mode 100644 index 00000000..c53272e2 --- /dev/null +++ b/src/Resources/views/Template/form.html.twig @@ -0,0 +1,13 @@ +
+
{{ 'bitbag_sylius_cms_plugin.ui.use_page_template'|trans }}
+
+
+ {{ form_row(form.template) }} +
+ +
+
From ad8fd2123c82340c3813ddb73f46fc21d12f2b25 Mon Sep 17 00:00:00 2001 From: jkindly Date: Thu, 1 Aug 2024 11:24:12 +0200 Subject: [PATCH 3/7] OP-463: Unit tests --- .../test_it_finds_template_by_name.yml | 9 +++++++++ .../Repository/TemplateRepositoryTest.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/Integration/DataFixtures/ORM/TemplateRepositoryTest/test_it_finds_template_by_name.yml b/tests/Integration/DataFixtures/ORM/TemplateRepositoryTest/test_it_finds_template_by_name.yml index 184c5df1..0b25ada4 100644 --- a/tests/Integration/DataFixtures/ORM/TemplateRepositoryTest/test_it_finds_template_by_name.yml +++ b/tests/Integration/DataFixtures/ORM/TemplateRepositoryTest/test_it_finds_template_by_name.yml @@ -8,3 +8,12 @@ BitBag\SyliusCmsPlugin\Entity\Template: template3: name: 'template3-name' type: 'page' + template4: + name: 'template4-name' + type: 'block' + template5: + name: 'template5-name' + type: 'block' + template6: + name: 'template6-name' + type: 'block' diff --git a/tests/Integration/Repository/TemplateRepositoryTest.php b/tests/Integration/Repository/TemplateRepositoryTest.php index ad10ad89..9608cfd7 100644 --- a/tests/Integration/Repository/TemplateRepositoryTest.php +++ b/tests/Integration/Repository/TemplateRepositoryTest.php @@ -36,6 +36,20 @@ public function test_it_finds_template_page_by_name_part(): void self::assertCount(3, $template); } + public function test_it_finds_template_block_by_name_part(): void + { + $this->loadFixturesFromFile('TemplateRepositoryTest/test_it_finds_template_by_name.yml'); + + $repository = $this->getRepository(); + + $phrase = 'template'; + $type = 'block'; + $template = $repository->findTemplatesByNamePart($phrase, $type); + + self::assertIsArray($template); + self::assertCount(3, $template); + } + private function getRepository(): TemplateRepositoryInterface { /** @var TemplateRepositoryInterface $repository */ From 3ac5538c45aaae792b54d9f4c7f26acb06a98a64 Mon Sep 17 00:00:00 2001 From: jkindly Date: Thu, 1 Aug 2024 12:13:01 +0200 Subject: [PATCH 4/7] OP-463: Behat tests --- features/admin/adding_block.feature | 13 ++++++++ features/admin/adding_template.feature | 10 +++++- tests/Behat/Context/Ui/Admin/BlockContext.php | 24 ++++++++++++++ tests/Behat/Page/Admin/Block/CreatePage.php | 33 +++++++++++++++++++ .../Page/Admin/Block/CreatePageInterface.php | 6 ++++ .../Page/Admin/Page/CreatePageInterface.php | 2 ++ .../Resources/suites/ui/managing_blocks.yml | 1 + 7 files changed, 88 insertions(+), 1 deletion(-) diff --git a/features/admin/adding_block.feature b/features/admin/adding_block.feature index 61550185..29dd78df 100644 --- a/features/admin/adding_block.feature +++ b/features/admin/adding_block.feature @@ -180,3 +180,16 @@ Feature: Adding blocks And I try to add it Then I should be notified that "Code, Name" fields are too long + @ui @javascript + Scenario: Adding block with template + Given there is an existing template named "Homepage" with "Block" type that contains "Textarea, Single media" content elements + When I go to the create block page + And I fill the code with "intro" + And I fill the name with "Intro" + And I select "Homepage" template + And I click button to use this template + And I confirm that I want to use this template + And I add it + Then I should be notified that the block has been created + And I should see newly created "Textarea" content element in Content elements section + And I should see newly created "Single media" content element in Content elements section diff --git a/features/admin/adding_template.feature b/features/admin/adding_template.feature index ef850a27..3197fb24 100644 --- a/features/admin/adding_template.feature +++ b/features/admin/adding_template.feature @@ -9,13 +9,21 @@ Feature: Adding cms templates And I am logged in as an administrator @ui - Scenario: Creating template + Scenario: Creating template with type page When I go to the create template page And I fill the name with "Test template" And I choose "Page" in Type field And I add it Then I should be notified that the template has been created + @ui + Scenario: Creating template with type block + When I go to the create template page + And I fill the name with "Test template" + And I choose "Block" in Type field + And I add it + Then I should be notified that the template has been created + @ui @javascript Scenario: Creating template with content elements When I go to the create template page diff --git a/tests/Behat/Context/Ui/Admin/BlockContext.php b/tests/Behat/Context/Ui/Admin/BlockContext.php index 607c2e30..ab024435 100755 --- a/tests/Behat/Context/Ui/Admin/BlockContext.php +++ b/tests/Behat/Context/Ui/Admin/BlockContext.php @@ -406,6 +406,30 @@ public function iShouldSeeEmptyListOfBlocks(): void $this->resolveCurrentPage()->isEmpty(); } + /** + * @Then I select :templateName template + */ + public function iSelectTemplate(string $templateName): void + { + $this->resolveCurrentPage()->selectTemplate($templateName); + } + + /** + * @Then I click button to use this template + */ + public function iClickButtonToUseThisTemplate(): void + { + $this->resolveCurrentPage()->useTemplate(); + } + + /** + * @Then I confirm that I want to use this template + */ + public function iConfirmThatIWantToUseThisTemplate(): void + { + $this->resolveCurrentPage()->confirmUseTemplate(); + } + /** * @return IndexPageInterface|CreatePageInterface|UpdatePageInterface|SymfonyPageInterface */ diff --git a/tests/Behat/Page/Admin/Block/CreatePage.php b/tests/Behat/Page/Admin/Block/CreatePage.php index 6e07b44b..a1dd1db6 100755 --- a/tests/Behat/Page/Admin/Block/CreatePage.php +++ b/tests/Behat/Page/Admin/Block/CreatePage.php @@ -255,6 +255,37 @@ public function addTaxonsListContentElementWithTaxons(array $taxons): void } } + public function selectTemplate(string $templateName): void + { + $dropdown = $this->getElement('template_select_dropdown'); + $dropdown->click(); + + $dropdown->waitFor(5, function () use ($templateName): bool { + return $this->hasElement('template_select_dropdown_item', [ + '%item%' => $templateName, + ]); + }); + + $item = $this->getElement('template_select_dropdown_item', [ + '%item%' => $templateName, + ]); + + $item->click(); + } + + public function useTemplate(): void + { + $this->getDocument()->findLink('Use this template')->click(); + } + + public function confirmUseTemplate(): void + { + $this->getDocument()->findById('load-template-confirmation-button')->click(); + $this->getDocument()->waitFor(2, function (): bool { + return '' !== $this->getDocument()->find('css', '[data-form-collection="list"]')->getHtml(); + }); + } + protected function getDefinedElements(): array { return array_merge( @@ -264,6 +295,8 @@ protected function getDefinedElements(): array 'association_dropdown_collection' => '.field > label:contains("Collections") ~ .sylius-autocomplete', 'association_dropdown_collection_item' => '.field > label:contains("Collections") ~ .sylius-autocomplete > div.menu > div.item:contains("%item%")', 'content_elements_add_button' => '#bitbag_sylius_cms_plugin_block_contentElements a[data-form-collection="add"]', + 'template_select_dropdown' => 'h5:contains("Use page template") ~ .column .field > .sylius-autocomplete', + 'template_select_dropdown_item' => 'h5:contains("Use page template") ~ .column .field > .sylius-autocomplete > div.menu > div.item:contains("%item%")', ], ); } diff --git a/tests/Behat/Page/Admin/Block/CreatePageInterface.php b/tests/Behat/Page/Admin/Block/CreatePageInterface.php index 54605051..24bda1f8 100755 --- a/tests/Behat/Page/Admin/Block/CreatePageInterface.php +++ b/tests/Behat/Page/Admin/Block/CreatePageInterface.php @@ -52,4 +52,10 @@ public function addProductsGridByTaxonContentElementWithTaxon(string $taxon): vo public function addTaxonsListContentElementWithTaxons(array $taxons): void; public function disable(): void; + + public function selectTemplate(string $templateName): void; + + public function useTemplate(): void; + + public function confirmUseTemplate(): void; } diff --git a/tests/Behat/Page/Admin/Page/CreatePageInterface.php b/tests/Behat/Page/Admin/Page/CreatePageInterface.php index adc032b6..3e5cc053 100755 --- a/tests/Behat/Page/Admin/Page/CreatePageInterface.php +++ b/tests/Behat/Page/Admin/Page/CreatePageInterface.php @@ -60,4 +60,6 @@ public function addTaxonsListContentElementWithTaxons(array $taxons): void; public function selectTemplate(string $templateName): void; public function useTemplate(): void; + + public function confirmUseTemplate(): void; } diff --git a/tests/Behat/Resources/suites/ui/managing_blocks.yml b/tests/Behat/Resources/suites/ui/managing_blocks.yml index 1645aa51..ea514393 100755 --- a/tests/Behat/Resources/suites/ui/managing_blocks.yml +++ b/tests/Behat/Resources/suites/ui/managing_blocks.yml @@ -11,6 +11,7 @@ default: - bitbag_sylius_cms_plugin.behat.context.setup.block - bitbag_sylius_cms_plugin.behat.context.setup.collection - bitbag_sylius_cms_plugin.behat.context.setup.media + - bitbag_sylius_cms_plugin.behat.context.setup.template - bitbag_sylius_cms_plugin.behat.context.ui.admin.block filters: From 57785404dbcbf70ecb4c91ead3a7661449471647 Mon Sep 17 00:00:00 2001 From: jkindly Date: Fri, 2 Aug 2024 08:53:50 +0200 Subject: [PATCH 5/7] OP-463: Move CMS menu under marketing --- src/Menu/ContentManagementMenuBuilder.php | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Menu/ContentManagementMenuBuilder.php b/src/Menu/ContentManagementMenuBuilder.php index 8862823f..c0e25f11 100755 --- a/src/Menu/ContentManagementMenuBuilder.php +++ b/src/Menu/ContentManagementMenuBuilder.php @@ -10,6 +10,7 @@ namespace BitBag\SyliusCmsPlugin\Menu; +use Knp\Menu\ItemInterface; use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent; final class ContentManagementMenuBuilder @@ -62,5 +63,29 @@ public function buildMenu(MenuBuilderEvent $menuBuilderEvent): void ->setLabel('bitbag_sylius_cms_plugin.ui.media') ->setLabelAttribute('icon', 'file') ; + + $this->reorderMenu($menu, 'bitbag_cms', 'marketing'); + } + + private function reorderMenu(ItemInterface $menu, string $newItemKey, string $targetItemKey): void + { + $menuItems = $menu->getChildren(); + + $newMenuItem = $menu->getChild($newItemKey); + unset($menuItems[$newItemKey]); + + $targetPosition = array_search($targetItemKey, array_keys($menuItems), true); + + if (null !== $newMenuItem && false !== $targetPosition) { + $menuItems = array_slice($menuItems, 0, $targetPosition + 1, true) + + [$newItemKey => $newMenuItem] + + array_slice($menuItems, $targetPosition + 1, null, true); + + $menuItems = array_filter($menuItems, static function ($item) { + return $item instanceof ItemInterface; + }); + + $menu->setChildren($menuItems); + } } } From 684dc1864c4ae5af4d8c4ad460f072d0f59ea85e Mon Sep 17 00:00:00 2001 From: jkindly Date: Fri, 2 Aug 2024 09:08:23 +0200 Subject: [PATCH 6/7] OP-463: PHPSpec menu correction --- spec/Menu/ContentManagementMenuBuilderSpec.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/Menu/ContentManagementMenuBuilderSpec.php b/spec/Menu/ContentManagementMenuBuilderSpec.php index 19273075..c964b651 100755 --- a/spec/Menu/ContentManagementMenuBuilderSpec.php +++ b/spec/Menu/ContentManagementMenuBuilderSpec.php @@ -65,6 +65,11 @@ public function it_build_menu( $cmsRootMenuItem->setLabel('bitbag_sylius_cms_plugin.ui.media')->willReturn($cmsRootMenuItem); $cmsRootMenuItem->setLabelAttribute('icon', 'file')->shouldBeCalled(); + $menu->getChildren()->willReturn(['marketing' => $cmsRootMenuItem]); + $menu->getChild('bitbag_cms')->willReturn($cmsRootMenuItem); + + $menu->setChildren(['marketing' => $cmsRootMenuItem, 'bitbag_cms' => $cmsRootMenuItem])->willReturn($menu); + $this->buildMenu($menuBuilderEvent); } } From fab07778771255325a3f4d2379b58b3aeb9afe6b Mon Sep 17 00:00:00 2001 From: jkindly Date: Mon, 12 Aug 2024 14:33:06 +0200 Subject: [PATCH 7/7] OP-463: Extract menu reorder as service + specs --- .../Menu/ContentManagementMenuBuilderSpec.php | 6 + spec/Menu/MenuReorderSpec.php | 106 ++++++++++++++++++ src/Menu/ContentManagementMenuBuilder.php | 29 +---- src/Menu/MenuReorder.php | 38 +++++++ src/Menu/MenuReorderInterface.php | 18 +++ .../config/services/event_listener.xml | 1 + src/Resources/config/services/menu.xml | 9 ++ 7 files changed, 183 insertions(+), 24 deletions(-) create mode 100644 spec/Menu/MenuReorderSpec.php create mode 100644 src/Menu/MenuReorder.php create mode 100644 src/Menu/MenuReorderInterface.php create mode 100644 src/Resources/config/services/menu.xml diff --git a/spec/Menu/ContentManagementMenuBuilderSpec.php b/spec/Menu/ContentManagementMenuBuilderSpec.php index c964b651..ba7b7188 100755 --- a/spec/Menu/ContentManagementMenuBuilderSpec.php +++ b/spec/Menu/ContentManagementMenuBuilderSpec.php @@ -11,12 +11,18 @@ namespace spec\BitBag\SyliusCmsPlugin\Menu; use BitBag\SyliusCmsPlugin\Menu\ContentManagementMenuBuilder; +use BitBag\SyliusCmsPlugin\Menu\MenuReorderInterface; use Knp\Menu\ItemInterface; use PhpSpec\ObjectBehavior; use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent; final class ContentManagementMenuBuilderSpec extends ObjectBehavior { + public function let(MenuReorderInterface $menuReorder): void + { + $this->beConstructedWith($menuReorder); + } + public function it_is_initializable(): void { $this->shouldHaveType(ContentManagementMenuBuilder::class); diff --git a/spec/Menu/MenuReorderSpec.php b/spec/Menu/MenuReorderSpec.php new file mode 100644 index 00000000..3eaa8c8c --- /dev/null +++ b/spec/Menu/MenuReorderSpec.php @@ -0,0 +1,106 @@ +shouldHaveType(MenuReorder::class); + } + + public function it_implements_menu_reorder_interface(): void + { + $this->shouldImplement(MenuReorderInterface::class); + } + + public function it_reorders_menu_items( + ItemInterface $menu, + ItemInterface $item1, + ItemInterface $item2, + ItemInterface $item3 + ): void + { + $menu->getChildren()->willReturn([ + 'item1' => $item1, + 'item2' => $item2, + 'item3' => $item3 + ]); + + $menu->getChild('item2')->willReturn($item2); + + $menu->setChildren([ + 'item1' => $item1, + 'item3' => $item3, + 'item2' => $item2 + ])->shouldBeCalled(); + + $this->reorder($menu, 'item2', 'item3'); + } + + public function it_does_not_reorder_if_new_item_is_not_found( + ItemInterface $menu, + ItemInterface $item1, + ItemInterface $item3 + ): void + { + $menu->getChildren()->willReturn([ + 'item1' => $item1, + 'item3' => $item3 + ]); + + $menu->getChild('item2')->willReturn(null); + $menu->setChildren(Argument::any())->shouldNotBeCalled(); + + $this->reorder($menu, 'item2', 'item3'); + } + + public function it_does_not_reorder_if_target_item_is_not_found( + ItemInterface $menu, + ItemInterface $item1, + ItemInterface $item2 + ): void + { + $menu->getChildren()->willReturn([ + 'item1' => $item1, + 'item2' => $item2 + ]); + + $menu->getChild('item1')->willReturn($item1); + $menu->setChildren(Argument::any())->shouldNotBeCalled(); + + $this->reorder($menu, 'item1', 'item3'); + } + + public function it_does_not_modify_menu_when_no_reorder_is_needed( + ItemInterface $menu, + ItemInterface $item1, + ItemInterface $item2 + ): void + { + $menu->getChildren()->willReturn([ + 'item1' => $item1, + 'item2' => $item2 + ]); + + $menu->getChild('item1')->willReturn($item1); + $menu->getChild('item2')->willReturn($item2); + $menu->setChildren(Argument::any())->shouldNotBeCalled(); + + $this->reorder($menu, 'item1', 'item1'); + } +} diff --git a/src/Menu/ContentManagementMenuBuilder.php b/src/Menu/ContentManagementMenuBuilder.php index c0e25f11..53763702 100755 --- a/src/Menu/ContentManagementMenuBuilder.php +++ b/src/Menu/ContentManagementMenuBuilder.php @@ -10,11 +10,14 @@ namespace BitBag\SyliusCmsPlugin\Menu; -use Knp\Menu\ItemInterface; use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent; final class ContentManagementMenuBuilder { + public function __construct(private MenuReorderInterface $menuReorder) + { + } + public function buildMenu(MenuBuilderEvent $menuBuilderEvent): void { $menu = $menuBuilderEvent->getMenu(); @@ -64,28 +67,6 @@ public function buildMenu(MenuBuilderEvent $menuBuilderEvent): void ->setLabelAttribute('icon', 'file') ; - $this->reorderMenu($menu, 'bitbag_cms', 'marketing'); - } - - private function reorderMenu(ItemInterface $menu, string $newItemKey, string $targetItemKey): void - { - $menuItems = $menu->getChildren(); - - $newMenuItem = $menu->getChild($newItemKey); - unset($menuItems[$newItemKey]); - - $targetPosition = array_search($targetItemKey, array_keys($menuItems), true); - - if (null !== $newMenuItem && false !== $targetPosition) { - $menuItems = array_slice($menuItems, 0, $targetPosition + 1, true) + - [$newItemKey => $newMenuItem] + - array_slice($menuItems, $targetPosition + 1, null, true); - - $menuItems = array_filter($menuItems, static function ($item) { - return $item instanceof ItemInterface; - }); - - $menu->setChildren($menuItems); - } + $this->menuReorder->reorder($menu, 'bitbag_cms', 'marketing'); } } diff --git a/src/Menu/MenuReorder.php b/src/Menu/MenuReorder.php new file mode 100644 index 00000000..283d3702 --- /dev/null +++ b/src/Menu/MenuReorder.php @@ -0,0 +1,38 @@ +getChildren(); + + $newMenuItem = $menu->getChild($newItemKey); + unset($menuItems[$newItemKey]); + + $targetPosition = array_search($targetItemKey, array_keys($menuItems), true); + + if (null !== $newMenuItem && false !== $targetPosition) { + $menuItems = array_slice($menuItems, 0, $targetPosition + 1, true) + + [$newItemKey => $newMenuItem] + + array_slice($menuItems, $targetPosition + 1, null, true); + + $menuItems = array_filter($menuItems, static function ($item) { + return $item instanceof ItemInterface; + }); + + $menu->setChildren($menuItems); + } + } +} diff --git a/src/Menu/MenuReorderInterface.php b/src/Menu/MenuReorderInterface.php new file mode 100644 index 00000000..674b4be6 --- /dev/null +++ b/src/Menu/MenuReorderInterface.php @@ -0,0 +1,18 @@ + + diff --git a/src/Resources/config/services/menu.xml b/src/Resources/config/services/menu.xml new file mode 100644 index 00000000..54912252 --- /dev/null +++ b/src/Resources/config/services/menu.xml @@ -0,0 +1,9 @@ + + + + + + + + +