From 2b27e5054c2c3da127691579910a4b27476c5bd0 Mon Sep 17 00:00:00 2001 From: Lukasz Cybula Date: Thu, 4 Jan 2018 12:38:18 +0100 Subject: [PATCH] Fix for removable-only collections --- .travis.yml | 7 +++-- Behat/Context/FormContext.php | 27 +++++++++++++++++++ Resources/public/js/collection.js | 6 ++--- Resources/views/Form/form.html.twig | 2 +- .../Resources/translations/messages.en.yml | 1 + .../src/FSi/FixturesBundle/Form/NewsType.php | 9 +++++++ features/form/collections.feature | 5 ++++ 7 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 270f38c1..d5b6f5d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,15 @@ language: php -sudo: false +sudo: required cache: directories: - $HOME/.composer/cache - vendor +addons: + firefox: "47.0.1" + matrix: include: - php: 5.4 @@ -29,7 +32,7 @@ before_install: - composer self-update - if [[ $SYMFONY_VERSION ]]; then composer require symfony/symfony:${SYMFONY_VERSION} -n --no-update; fi; - composer update $COMPOSER_FLAGS - - "[ ! -f bin/selenium.jar ] && wget wget -O bin/selenium.jar http://selenium-release.storage.googleapis.com/2.50/selenium-server-standalone-2.50.1.jar || true" + - "[ ! -f bin/selenium.jar ] && wget wget -O bin/selenium.jar http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar || true" before_script: - nohup php -S localhost:8080 -t features/fixtures/project/web > /dev/null 2>&1 & diff --git a/Behat/Context/FormContext.php b/Behat/Context/FormContext.php index 88248cd2..7937d5eb 100644 --- a/Behat/Context/FormContext.php +++ b/Behat/Context/FormContext.php @@ -76,6 +76,7 @@ public function iFillFormFields(TableNode $table) /** * @Transform /"([^"]*)" non-editable collection/ * @Transform /non-editable collection "([^"]*)"/ + * @Transform /removable-only collection "([^"]*)"/ */ public function transformToNoneditableCollection($collectionNames) { @@ -96,6 +97,7 @@ public function transformToCollection($collectionNames) * @Then /^("[^"]*" collection) should have (\d+) (element|elements)$/ * @Given /^(non-editable collection "[^"]*") has (\d+) (element|elements)$/ * @Then /^(non-editable collection "[^"]*") should have (\d+) (element|elements)$/ + * @Then /^(removable-only collection "[^"]*") should have (\d+) (element|elements)$/ */ public function collectionShouldHaveElements(NodeElement $collection, $elementsCount) { @@ -128,6 +130,30 @@ public function allCollectionButtonsDisabled(NodeElement $collection) } } + /** + * @Then /^button for adding item in (removable-only collection "[^"]*") should be disabled$/ + */ + public function collectionAddButtonIsDisabled(NodeElement $collection) + { + $addButtons = $collection->findAll('css', '.collection-add'); + expect(count($addButtons))->notToBe(0); + foreach ($addButtons as $addButton) { + expect($addButton->hasClass('disabled'))->toBe(true); + } + } + + /** + * @Then /^buttons for removing items in (removable-only collection "[^"]*") should be enabled/ + */ + public function collectionRemoveButtonsAreEnabled(NodeElement $collection) + { + $addButtons = $collection->findAll('css', '.collection-remove'); + expect(count($addButtons))->notToBe(0); + foreach ($addButtons as $addButton) { + expect($addButton->hasClass('disabled'))->toBe(false); + } + } + /** * @When /^I press "([^"]*)" in (collection "[^"]*")$/ */ @@ -149,6 +175,7 @@ public function iFillWithInCollectionAtPosition($fieldName, $fieldValue, NodeEle /** * @Given /^I remove (\w+) element in (collection "[^"]*")$/ + * @Given /^I remove (\w+) element in (removable-only collection "[^"]*")$/ */ public function iRemoveElementInCollection($index, NodeElement $collection) { diff --git a/Resources/public/js/collection.js b/Resources/public/js/collection.js index 703198c7..9087abd4 100644 --- a/Resources/public/js/collection.js +++ b/Resources/public/js/collection.js @@ -7,7 +7,7 @@ define(['jquery'], function($) { removeButtonSelector: '> div > div > div > .collection-remove', itemsContainerSelector: '> .collection-items', itemSelector: '.form-group', - collectionsSelector: 'div[data-prototype]' + collectionsSelector: 'div[data-prototype-name]' }, options); var replacePrototypeName = function(template, prototypeName, index) { @@ -40,7 +40,7 @@ define(['jquery'], function($) { this.each(function(index, element) { var $el = $(element); - if (!$el.data('prototype') || $el.data('current-index')) { + if (!$el.data('prototype-name') || $el.data('current-index')) { return; } @@ -51,7 +51,7 @@ define(['jquery'], function($) { $el.on('click', options.removeButtonSelector, removeCollectionItem); } - if ($el.data('allow-add')) { + if ($el.data('allow-add') && $el.data('prototype')) { $el.find(options.addButtonSelector).on('click', addCollectionItem); } }); diff --git a/Resources/views/Form/form.html.twig b/Resources/views/Form/form.html.twig index 2eb82a86..74a7bcc2 100644 --- a/Resources/views/Form/form.html.twig +++ b/Resources/views/Form/form.html.twig @@ -48,7 +48,7 @@ $(label).append(' '); } }); - $context.find('div[data-prototype]') + $context.find('div[data-prototype-name]') .formCollection() .on('add.collection-item', function(event, item) { initForms($(item)); diff --git a/features/fixtures/project/app/Resources/translations/messages.en.yml b/features/fixtures/project/app/Resources/translations/messages.en.yml index 7cdb3a6a..0ac8aa42 100644 --- a/features/fixtures/project/app/Resources/translations/messages.en.yml +++ b/features/fixtures/project/app/Resources/translations/messages.en.yml @@ -20,6 +20,7 @@ admin: tags: Tags tag_elements: Elements non_editable_tags: Non-editable tags + removable_comments: Removable-only comments actions: Actions subscriber: name: Subscribers diff --git a/features/fixtures/project/src/FSi/FixturesBundle/Form/NewsType.php b/features/fixtures/project/src/FSi/FixturesBundle/Form/NewsType.php index d43e495f..979fc5ea 100644 --- a/features/fixtures/project/src/FSi/FixturesBundle/Form/NewsType.php +++ b/features/fixtures/project/src/FSi/FixturesBundle/Form/NewsType.php @@ -50,6 +50,15 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'mapped' => false, 'required' => false ]); + $builder->add('removableComments', 'collection', [ + 'type' => 'text', + 'data' => new ArrayCollection(['Comment 1', 'Comment 2', 'Comment 3']), + 'label' => 'admin.news.list.removable_comments', + 'allow_add' => false, + 'allow_delete' => true, + 'mapped' => false, + 'required' => false + ]); } public function getName() diff --git a/features/form/collections.feature b/features/form/collections.feature index 8aac4276..91a78997 100644 --- a/features/form/collections.feature +++ b/features/form/collections.feature @@ -63,4 +63,9 @@ Feature: Managing form collections Given there is a "news" with "title" "News 1" present in the database And I am on the "News edit" page with id "1" Then non-editable collection "Non-editable tags" should have 3 elements + And removable-only collection "Removable-only comments" should have 3 elements And all buttons for adding and removing items in non-editable collection "Non-editable tags" should be disabled + And button for adding item in removable-only collection "Removable-only comments" should be disabled + And buttons for removing items in removable-only collection "Removable-only comments" should be enabled + When I remove first element in removable-only collection "Removable-only comments" + Then removable-only collection "Removable-only comments" should have 2 elements