From c47ad68586749e19fce125da9820d0fd0b2bf575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 26 Oct 2015 20:59:33 +0100 Subject: [PATCH 01/15] add gitlab-ci --- .gitlab-ci.yml | 17 +++++++++++++++++ .../App/app/config/parameters.gitlab-ci.yml | 7 +++++++ 2 files changed, 24 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 000000000..6a6ef31ee --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,17 @@ +services: + - chill/database:latest + +before_script: + - composer config -g github-oauth.github.com $GITHUB_TOKEN + - composer install --no-interaction + - cp Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml Tests/Fixtures/App/app/config/parameters.yml + - ./console.sh --env=test cache:warmup + - ./console.sh doctrine:migrations:migrate --env=test --no-interaction + - ./console.sh doctrine:fixtures:load --env=test --no-interaction + +test:php-5.6: + stage: test + script: phpunit --testdox +test:php-7: + stage: test + script: phpunit --testdox diff --git a/Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml b/Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml new file mode 100644 index 000000000..01979d64f --- /dev/null +++ b/Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml @@ -0,0 +1,7 @@ +parameters: + database_host: localhost + database_port: 5432 + database_name: postgres + database_user: postgres + database_password: postgres + locale: fr From 7baa7bddc3a1613816c691e454841baa885a5095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 26 Oct 2015 21:13:47 +0100 Subject: [PATCH 02/15] fix auth and database url --- .gitignore | 2 ++ .gitlab-ci.yml | 2 +- Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 90d5ec78f..08abe3eac 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ bin/* /tmp/* src/Chill/CustomFieldsBundle/vendor/* bootstrap.php.cache +#the file created by composer to store creds +auth.json diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6a6ef31ee..9f2233cb5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,7 @@ services: - chill/database:latest before_script: - - composer config -g github-oauth.github.com $GITHUB_TOKEN + - composer config github-oauth.github.com $GITHUB_TOKEN - composer install --no-interaction - cp Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml Tests/Fixtures/App/app/config/parameters.yml - ./console.sh --env=test cache:warmup diff --git a/Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml b/Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml index 01979d64f..9e3b75daf 100644 --- a/Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml +++ b/Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml @@ -1,5 +1,5 @@ parameters: - database_host: localhost + database_host: chill__database database_port: 5432 database_name: postgres database_user: postgres From 3e5a90c36c5719b010b2ebde585e7d58d06090b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 23 Dec 2015 23:46:15 +0100 Subject: [PATCH 03/15] escaping argument --- Resources/config/services.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 1e03b8bf2..9af084616 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -124,5 +124,5 @@ services: class: Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice\OptionRepository factory: ["@doctrine", getRepository] arguments: - - "Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option" + - "Chill\\CustomFieldsBundle\\Entity\\CustomFieldLongChoice\\Option" \ No newline at end of file From 09a69765b98836bbfccfaf2695164fda55fa8b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 23 Dec 2015 23:46:44 +0100 Subject: [PATCH 04/15] adding test for customFieldRendering function --- .../Twig/CustomFieldRenderingTwigTest.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Tests/Templating/Twig/CustomFieldRenderingTwigTest.php diff --git a/Tests/Templating/Twig/CustomFieldRenderingTwigTest.php b/Tests/Templating/Twig/CustomFieldRenderingTwigTest.php new file mode 100644 index 000000000..7f4923349 --- /dev/null +++ b/Tests/Templating/Twig/CustomFieldRenderingTwigTest.php @@ -0,0 +1,105 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\CustomFields\Tests\Templating\Twig; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Chill\CustomFieldsBundle\Templating\Twig\CustomFieldRenderingTwig; +use Chill\CustomFieldsBundle\Entity\CustomField; +use Chill\CustomFieldsBundle\Service\CustomFieldProvider; + +/** + * Test the rendering of twig function which renders custom fields + * + * @author Julien Fastré + * @author Champs Libres + */ +class CustomFieldRenderingTwigTest extends KernelTestCase +{ + /** + * + * @var CustomFieldRenderingTwig + */ + private $cfRendering; + + /** + * + * @var CustomFieldProvider + */ + private $cfProvider; + + public function setUp() + { + self::bootKernel(); + $this->cfRendering = self::$kernel->getContainer() + ->get('chill.custom_field.twig.custom_fields_rendering') + ; + + $this->cfProvider = self::$kernel->getContainer() + ->get('chill.custom_field.provider'); + + // set locale to fr + $prophet = new \Prophecy\Prophet; + $request = $prophet->prophesize(); + $request->willExtend('Symfony\Component\HttpFoundation\Request'); + $request->getLocale()->willReturn('fr'); + self::$kernel->getContainer()->get('request_stack') + ->push($request->reveal()); + } + + /** + * + * @return CustomField + */ + private function getSimpleCustomFieldText() + { + return (new CustomField()) + ->setSlug('test') + ->setName(array('fr' => 'Test')) + ->setType('text') + ->setOrdering(10) + ->setOptions(array("maxLength" => 255)) + ->setActive(true) + ; + } + + public function testLabelRendering() + { + $cf = $this->getSimpleCustomFieldText(); + + $text = $this->cfRendering->renderLabel($cf); + + $this->assertContains('Test', $text, + "The rendering text should contains the 'test' text"); + } + + public function testWidgetRendering() + { + $cf = $this->getSimpleCustomFieldText(); + $fields = array( + 'test' => "My tailor is rich" + ); + + $text = $this->cfRendering->renderWidget($fields, $cf); + + $this->assertContains('My tailor is rich', $text, + "The rendering text should contains the 'test' text"); + } +} From 7a73c0170c7af36e78bbacc16a9a3387ad9169af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 24 Dec 2015 15:16:04 +0100 Subject: [PATCH 05/15] set a composer root version + select image php-7 The composer root version is required to let composer decide on which branch he is, as we have mirrored dependencies (custom field requires person, which requires custom field). The php 7 build select now php 7 image --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9f2233cb5..50a37acc8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,6 +3,7 @@ services: before_script: - composer config github-oauth.github.com $GITHUB_TOKEN + - if [ $CI_BUILD_REF_NAME = "1.0" ] ; then export COMPOSER_ROOT_VERSION="1.0-dev"; else export COMPOSER_ROOT_VERSION="dev-master"; fi - composer install --no-interaction - cp Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml Tests/Fixtures/App/app/config/parameters.yml - ./console.sh --env=test cache:warmup @@ -14,4 +15,5 @@ test:php-5.6: script: phpunit --testdox test:php-7: stage: test + image: chill/ci-image:php-7 script: phpunit --testdox From f7dd99ab87008766d9102e5fc111996fdca394ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 24 Dec 2015 16:05:49 +0100 Subject: [PATCH 06/15] add test for custom_fields_group_widget --- .../CustomFieldsGroupRenderingTwigTest.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Tests/Templating/Twig/CustomFieldsGroupRenderingTwigTest.php diff --git a/Tests/Templating/Twig/CustomFieldsGroupRenderingTwigTest.php b/Tests/Templating/Twig/CustomFieldsGroupRenderingTwigTest.php new file mode 100644 index 000000000..d1cbe0209 --- /dev/null +++ b/Tests/Templating/Twig/CustomFieldsGroupRenderingTwigTest.php @@ -0,0 +1,130 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\CustomFields\Tests\Templating\Twig; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Chill\CustomFieldsBundle\Templating\Twig\CustomFieldsGroupRenderingTwig; +use Chill\CustomFieldsBundle\Entity\CustomField; +use Chill\CustomFieldsBundle\Service\CustomFieldProvider; +use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup; + +/** + * Test the rendering of a custom fields group through + * the `chill_custom_fields_group_widget` + * + * @author Julien Fastré + * @author Champs Libres + */ +class CustomFieldsGroupRenderingTwigTest extends KernelTestCase +{ + /** + * + * @var CustomFieldsGroupRenderingTwig + */ + private $cfRendering; + + /** + * + * @var CustomFieldProvider + */ + private $cfProvider; + + public function setUp() + { + self::bootKernel(); + $this->cfRendering = self::$kernel->getContainer() + ->get('chill.custom_field.twig.custom_fields_group_rendering') + ; + + $this->cfProvider = self::$kernel->getContainer() + ->get('chill.custom_field.provider'); + + // set locale to fr + $prophet = new \Prophecy\Prophet; + $request = $prophet->prophesize(); + $request->willExtend('Symfony\Component\HttpFoundation\Request'); + $request->getLocale()->willReturn('fr'); + self::$kernel->getContainer()->get('request_stack') + ->push($request->reveal()); + } + + /** + * + * @return CustomField + */ + private function getSimpleCustomFieldText($slug, $name) + { + return (new CustomField()) + ->setSlug($slug) + ->setName(array('fr' => $name)) + ->setType('text') + ->setOrdering(10) + ->setOptions(array("maxLength" => 255)) + ->setActive(true) + ; + } + + /** + * + * @return CustomFieldsGroup + */ + private function getCustomFieldsGroup() + { + return (new CustomFieldsGroup()) + ->setEntity('\Dummy') + ->setName(array("fr" => "A cf group")) + ->addCustomField($this->getSimpleCustomFieldText("horses", "Do you like horses ?.")) + ->addCustomField($this->getSimpleCustomFieldText("sure", "Are you sure ?")) + ; + } + + public function testRenderingWidget() + { + $cfGroup = $this->getCustomFieldsGroup(); + + $text = $this->cfRendering->renderWidget(array( + 'horses' => 'I like horses', + 'sure' => 'Yes !' + ), $cfGroup); + + $this->assertContains('Do you like horses', $text); + $this->assertContains('I like horses', $text); + $this->assertContains('Are you sure', $text); + $this->assertContains('Yes', $text); + } + + public function testRenderingWidgetDoNotShowEmpty() + { + $cfGroup = $this->getCustomFieldsGroup(); + $cfGroup->addCustomField($this->getSimpleCustomFieldText('empty', 'Do not answer')); + + $text = $this->cfRendering->renderWidget(array( + 'horses' => 'I like horses', + 'sure' => 'Yes !' + ), $cfGroup, 'html', array('show_empty' => false)); + + $this->assertContains('Do you like horses', $text); + $this->assertContains('I like horses', $text); + $this->assertContains('Are you sure', $text); + $this->assertContains('Yes', $text); + $this->assertNotContains('Do not answer', $text); + } +} From d2039893b3ca643e7ef0c194b0312885dd0268ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Dec 2015 00:38:27 +0100 Subject: [PATCH 07/15] Simplifiying Helper and twig functions Now, the CustomFieldsHelper methods and the twig function which render customFields require always a `Chill\CustomFields\Entity\CustomField`. The possibility to add a slug and an object as argument to those method is abandoned, and was not used in other bundle (nevertheless, the method to catch a customField from his slug was buggy). --- Service/CustomFieldsHelper.php | 88 +------------------ Service/CustomFieldsHelperException.php | 4 +- Templating/Twig/CustomFieldRenderingTwig.php | 18 ++-- Tests/Service/CustomFieldsHelperTest.php | 90 ++++++++++++++++++++ 4 files changed, 102 insertions(+), 98 deletions(-) create mode 100644 Tests/Service/CustomFieldsHelperTest.php diff --git a/Service/CustomFieldsHelper.php b/Service/CustomFieldsHelper.php index 07c8c4f04..7baa48616 100644 --- a/Service/CustomFieldsHelper.php +++ b/Service/CustomFieldsHelper.php @@ -45,10 +45,6 @@ class CustomFieldsHelper * fields */ private $provider; - /** @var array $cache Matrix to cache all the custom fields. The array - * is indexed by the EntityClass then the slug */ - private $cache = array(); - /** * Constructor * @@ -63,83 +59,8 @@ class CustomFieldsHelper $this->provider = $provider; } - /** - * Set in cache all the custom fields of a given class containing some - * custom fields. - * - * @param object|string $class The given class. - * @todo check if this fucntions has to call _cacheCustomFieldsGroup instead of - * _cacheCustomFields ? - */ - private function _cacheCustomFields($class) + public function isEmptyValue(array $fields, CustomField $customField) { - $customFieldsGroups = $this->em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup') - ->findBy(array('entity' => (is_string($class)) ? $class : get_class($class))); - - if (!$customFieldsGroups) { - throw CustomFieldsHelperException::customFieldsGroupNotFound((is_string($class)) ? $class : get_class($class)); - } - - foreach ($customFieldsGroup as $cfGroup) { - $this->_cacheCustomFields($cfGroup); - } - } - - /** - * Set in cache of the custom fields of a customfield Group. - * - * @param CustomFieldsGroup $group The given CustomFieldsGroup - */ - private function _cacheCustomFieldsGroup(CustomFieldsGroup $group) - { - foreach ($group->getCustomFields() as $field) { - $this->cache[$group->getEntity()][$field->getSlug()] = $field; - } - } - - /** - * Return a requested customField - * - * @param object|string $class The requested class - * @param string $slug The slug. BEWARE If the slug is null, throw a proper CustomFieldsHelperException - * @return CustomField The requested CustomField - * @throws CustomFieldsHelperException if $slug is null - */ - public function getCustomField($class, $slug = null) - { - if (!$slug) { - throw CustomFieldsHelperException::slugIsMissing(); - } - - $resolveClass = (is_string($class)) ? $class : get_class($class); - if (!$this->cache[$resolveClass][$slug]) { - $this->_cacheCustomFields($resolveClass); - } - - return $this->cache[$resolveClass][$slug]; - } - - /** - * Return the stored/raw value of a custom field. - * - * The method return null if the slug is not recorded. - * - * @param array $fields the **raw** array, as stored in the db - * @param object|string $class - * @param string $slug - * @return mixed|null The value or null if the slug is not recorded - */ - private function getCustomFieldValue(array $fields, $class, $slug) - { - return (isset($fields[$slug])) ? $this->provider - ->getCustomFieldByType($this->getCustomField($class, $slug)->getType()) - ->deserialize($fields[$slug]) - : null; - } - - public function isEmptyValue(array $fields, $classOrCustomField, $slug = null) - { - $customField = ($classOrCustomField instanceof CustomField) ? $classOrCustomField : $this->getCustomField($classOrCustomField, $slug); $slug = $customField->getSlug(); $rawValue = (isset($fields[$slug])) ? $fields[$slug] : null; @@ -152,16 +73,13 @@ class CustomFieldsHelper * Render the value of a custom field * * @param array $fields the **raw** array, as stored in the db - * @param CustomField|object|string $classOrCustomField the object OR the get_class($object) string OR The CustomField + * @param CustomField $customField the customField entity * @param string $documentType The type of document in which the rendered value is displayed ('html' or 'csv'). - * @param string $slug The slug of the custom field to render. - * @param boolean $showIfEmpty If the widget must be rendered if the value is empty. An empty value is all values described as http://php.net/manual/fr/function.empty.php, except `FALSE` * @throws CustomFieldsHelperException if slug is missing * @return The representation of the value the customField. */ - public function renderCustomField(array $fields, $classOrCustomField, $documentType='html', $slug = null, $showIfEmpty = true) + public function renderCustomField(array $fields, CustomField $customField, $documentType='html') { - $customField = ($classOrCustomField instanceof CustomField) ? $classOrCustomField : $this->getCustomField($classOrCustomField, $slug); $slug = $customField->getSlug(); $rawValue = (isset($fields[$slug])) ? $fields[$slug] : null; $customFieldType = $this->provider->getCustomFieldByType($customField->getType()); diff --git a/Service/CustomFieldsHelperException.php b/Service/CustomFieldsHelperException.php index f43e5cc36..497c96720 100644 --- a/Service/CustomFieldsHelperException.php +++ b/Service/CustomFieldsHelperException.php @@ -26,11 +26,11 @@ class CustomFieldsHelperException extends \Exception { public static function customFieldsGroupNotFound($entity) { - return new CustomFieldsRenderingException("The customFieldsGroups associated with $entity are not found"); + return new CustomFieldsHelperException("The customFieldsGroups associated with $entity are not found"); } public static function slugIsMissing() { - return new CustomFieldsRenderingException("The slug is missing"); + return new CustomFieldsHelperException("The slug is missing"); } } \ No newline at end of file diff --git a/Templating/Twig/CustomFieldRenderingTwig.php b/Templating/Twig/CustomFieldRenderingTwig.php index 8561d26f8..32246cbd7 100644 --- a/Templating/Twig/CustomFieldRenderingTwig.php +++ b/Templating/Twig/CustomFieldRenderingTwig.php @@ -85,10 +85,10 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware } - public function isEmptyValue($customFieldorClass, $fields, $slug = null) + public function isEmptyValue(CustomField $customField, $fields, $slug = null) { return $this->container->get('chill.custom_field.helper') - ->isEmptyValue($fields, $customFieldorClass); + ->isEmptyValue($fields, $customField); } /* (non-PHPdoc) @@ -102,19 +102,15 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware /** * Twig Extension that is used to render the label of a custom field. * - * @param CustomField|object|string $customFieldOrClass Either a customField OR a customizable_entity OR the FQDN of the entity + * @param CustomField $customField Either a customField OR a customizable_entity OR the FQDN of the entity * @param string $slug The slug ONLY necessary if the first argument is NOT a CustomField instance * @param array $params The parameters for rendering. Currently, 'label_layout' allow to choose a different label. Default is 'ChillCustomFieldsBundle:CustomField:render_label.html.twig' * @return string HTML representation of the custom field label. */ - public function renderLabel($customFieldOrClass, $slug = null, array $params = array()) + public function renderLabel(CustomField $customField, $slug = null, array $params = array()) { $resolvedParams = array_merge($this->defaultParams, $params); - $customField = ($customFieldOrClass instanceof CustomField) - ? $customFieldOrClass : $this->container->get('chill.custom_field.provider') - ->getCustomField($customFieldOrClass, $slug); - return $this->container->get('templating') ->render($resolvedParams['label_layout'], array('customField' => $customField)); } @@ -125,14 +121,14 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware * The presentation of the value is influenced by the document type. * * @param array $fields The array raw, as stored in the db - * @param CustomField|object|string $customFieldOrClass Either a customField OR a customizable_entity OR the FQDN of the entity + * @param CustomField $customField Either a customField OR a customizable_entity OR the FQDN of the entity * @param string $documentType The type of the document (csv, html) * @param string $slug The slug of the custom field ONLY necessary if the first argument is NOT a CustomField instance * @return string HTML representation of the custom field value, as described in the CustomFieldInterface. Is HTML safe */ - public function renderWidget(array $fields, $customFieldOrClass, $documentType='html', $slug = null) + public function renderWidget(array $fields, CustomField $customField, $documentType='html') { return $this->container->get('chill.custom_field.helper') - ->renderCustomField($fields, $customFieldOrClass, $documentType, $slug); + ->renderCustomField($fields, $customField, $documentType); } } \ No newline at end of file diff --git a/Tests/Service/CustomFieldsHelperTest.php b/Tests/Service/CustomFieldsHelperTest.php new file mode 100644 index 000000000..82092bf1c --- /dev/null +++ b/Tests/Service/CustomFieldsHelperTest.php @@ -0,0 +1,90 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\CustomFields\Tests\Service; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Chill\CustomFieldsBundle\Service\CustomFieldsHelper; +use Chill\CustomFieldsBundle\Entity\CustomField; + +/** + * Tests for custom fields helper + * + * @author Julien Fastré + * @author Champs Libres + */ +class CustomFieldsHelperTest extends KernelTestCase +{ + /** + * + * @var CustomFieldsHelper + */ + private $cfHelper; + + /** + * + * @var CustomField + */ + private $randomCFText; + + public function setUp() + { + self::bootKernel(); + + $container = self::$kernel->getContainer(); + + $this->cfHelper = $container->get('chill.custom_field.helper'); + + $cfieldsText = $container->get('doctrine.orm.entity_manager') + ->getRepository('ChillCustomFieldsBundle:CustomField') + ->findBy(array('type' => 'text')) + ; + $this->randomCFText = end($cfieldsText); + } + + public function testRenderCustomField() + { + $data = array( + $this->randomCFText->getSlug() => 'Sample text' + ); + + $text = $this->cfHelper->renderCustomField($data, $this->randomCFText); + + $this->assertContains('Sample text', $text); + } + + public function testIsEmptyValue() + { + // not empty value + $data = array( + $this->randomCFText->getSlug() => 'Sample text' + ); + + $this->assertFalse($this->cfHelper->isEmptyValue($data, $this->randomCFText)); + + //empty value + $data = array( + $this->randomCFText->getSlug() => '' + ); + + $this->assertTrue($this->cfHelper->isEmptyValue($data, $this->randomCFText)); + } + +} From 54391b02f33bcf523cc1f4c026dae0bf8c137c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Dec 2015 00:54:38 +0100 Subject: [PATCH 08/15] consistency in twig methods The method chill_custom_fields_is_empty take now the fields as first argument, and the customField as second argument, to be consistent with other twig methods. --- .../views/CustomFieldsGroup/render.html.twig | 2 +- Templating/Twig/CustomFieldRenderingTwig.php | 2 +- .../Twig/CustomFieldRenderingTwigTest.php | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Resources/views/CustomFieldsGroup/render.html.twig b/Resources/views/CustomFieldsGroup/render.html.twig index fb293cf92..f726aa51d 100644 --- a/Resources/views/CustomFieldsGroup/render.html.twig +++ b/Resources/views/CustomFieldsGroup/render.html.twig @@ -22,7 +22,7 @@ {%- endif -%} {%- endif -%} {% else %} - {%- if show_empty == true or (chill_custom_field_is_empty(customField, cFData) == false) -%} + {%- if show_empty == true or (chill_custom_field_is_empty(cFData, customField) == false) -%} {%- if title is not empty -%} {{ chill_custom_field_widget(cFData, title) }} {%- set title = null -%} diff --git a/Templating/Twig/CustomFieldRenderingTwig.php b/Templating/Twig/CustomFieldRenderingTwig.php index 32246cbd7..de13b8efb 100644 --- a/Templating/Twig/CustomFieldRenderingTwig.php +++ b/Templating/Twig/CustomFieldRenderingTwig.php @@ -85,7 +85,7 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware } - public function isEmptyValue(CustomField $customField, $fields, $slug = null) + public function isEmptyValue($fields, CustomField $customField) { return $this->container->get('chill.custom_field.helper') ->isEmptyValue($fields, $customField); diff --git a/Tests/Templating/Twig/CustomFieldRenderingTwigTest.php b/Tests/Templating/Twig/CustomFieldRenderingTwigTest.php index 7f4923349..2f6f51df7 100644 --- a/Tests/Templating/Twig/CustomFieldRenderingTwigTest.php +++ b/Tests/Templating/Twig/CustomFieldRenderingTwigTest.php @@ -102,4 +102,27 @@ class CustomFieldRenderingTwigTest extends KernelTestCase $this->assertContains('My tailor is rich', $text, "The rendering text should contains the 'test' text"); } + + public function testIsEmpty() + { + $cf = $this->getSimpleCustomFieldText(); + + // value is not empty + $fields = array( + 'test' => "My tailor is rich" + ); + + $result = $this->cfRendering->isEmptyValue($fields, $cf); + + $this->assertFalse($result); + + // value is empty + $fields = array( + 'text' => '' + ); + + $result = $this->cfRendering->isEmptyValue($fields, $cf); + + $this->assertTrue($result); + } } From cad44ae46169465257b564d9abb8579808a144c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Dec 2015 20:54:34 +0100 Subject: [PATCH 09/15] fix test 'complete scenario' The link 'modifier' was changed to 'Modifier' --- Tests/Controller/CustomFieldsGroupControllerTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/Controller/CustomFieldsGroupControllerTest.php b/Tests/Controller/CustomFieldsGroupControllerTest.php index 420c4bf4c..c2150abec 100644 --- a/Tests/Controller/CustomFieldsGroupControllerTest.php +++ b/Tests/Controller/CustomFieldsGroupControllerTest.php @@ -51,7 +51,10 @@ class CustomFieldsGroupControllerTest extends WebTestCase private function editCustomFieldsGroup(Client $client) { $crawler = $client->request('GET', '/fr/admin/customfieldsgroup/'); - $links = $crawler->selectLink('modifier'); + $links = $crawler->selectLink('Modifier'); + + $this->assertGreaterThan(0, $links->count(), + "We can't find a 'Modifier' link on the index page"); $crawler = $client->click($links->last()->link()); $this->assertEquals(200, $client->getResponse()->getStatusCode()); From 3d2426d8dfd7482047c521b5549d620def8bb045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Dec 2015 20:59:19 +0100 Subject: [PATCH 10/15] allow php7 and upgrade to symfony 2.8 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 9c1240b5a..21ae7284d 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,8 @@ } ], "require": { - "php": "~5.5", - "symfony/symfony": "~2.7", + "php": "~5.5|~7.0", + "symfony/symfony": "~2.8", "doctrine/orm": "~2.4", "doctrine/dbal" : "~2.5", "doctrine/common": "~2.4", From 287c70c6f3013ee8bf03bd4551a07bbc2f40affb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Dec 2015 21:13:05 +0100 Subject: [PATCH 11/15] add validation rule on slug for custom fields --- Resources/config/validation.yml | 7 +++++++ Resources/translations/validators.fr.yml | 1 + 2 files changed, 8 insertions(+) create mode 100644 Resources/config/validation.yml create mode 100644 Resources/translations/validators.fr.yml diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml new file mode 100644 index 000000000..419f35a68 --- /dev/null +++ b/Resources/config/validation.yml @@ -0,0 +1,7 @@ +Chill\CustomFieldsBundle\Entity\CustomField: + properties: + slug: + - Regex: + pattern: '/^[0-9a-z\-]{1,}$/' + message: Characters not allowed. Only lowercase letters, numbers and "-" are allowed. + \ No newline at end of file diff --git a/Resources/translations/validators.fr.yml b/Resources/translations/validators.fr.yml new file mode 100644 index 000000000..afb6575ba --- /dev/null +++ b/Resources/translations/validators.fr.yml @@ -0,0 +1 @@ +Characters not allowed. Only lowercase letters, numbers and "-" are allowed.: Caractères non autorisés. Seules les lettres minuscules, les nombres et le tiret ("-") sont autorisés. \ No newline at end of file From 7a8ef70cd1def721eb0123f52040319820432088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 29 Dec 2015 17:04:58 +0100 Subject: [PATCH 12/15] fix renderLabel signature --- Templating/Twig/CustomFieldRenderingTwig.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Templating/Twig/CustomFieldRenderingTwig.php b/Templating/Twig/CustomFieldRenderingTwig.php index de13b8efb..a43d7dc26 100644 --- a/Templating/Twig/CustomFieldRenderingTwig.php +++ b/Templating/Twig/CustomFieldRenderingTwig.php @@ -103,11 +103,10 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware * Twig Extension that is used to render the label of a custom field. * * @param CustomField $customField Either a customField OR a customizable_entity OR the FQDN of the entity - * @param string $slug The slug ONLY necessary if the first argument is NOT a CustomField instance * @param array $params The parameters for rendering. Currently, 'label_layout' allow to choose a different label. Default is 'ChillCustomFieldsBundle:CustomField:render_label.html.twig' * @return string HTML representation of the custom field label. */ - public function renderLabel(CustomField $customField, $slug = null, array $params = array()) + public function renderLabel(CustomField $customField, array $params = array()) { $resolvedParams = array_merge($this->defaultParams, $params); From 546fe4996069a8c1f55e34b546a30cb10dc3a5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 29 Dec 2015 17:09:04 +0100 Subject: [PATCH 13/15] ignore platform reqs on build --- .gitlab-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 50a37acc8..51432808b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ services: before_script: - composer config github-oauth.github.com $GITHUB_TOKEN - if [ $CI_BUILD_REF_NAME = "1.0" ] ; then export COMPOSER_ROOT_VERSION="1.0-dev"; else export COMPOSER_ROOT_VERSION="dev-master"; fi - - composer install --no-interaction + - composer install --no-interaction --ignore-platform-reqs - cp Tests/Fixtures/App/app/config/parameters.gitlab-ci.yml Tests/Fixtures/App/app/config/parameters.yml - ./console.sh --env=test cache:warmup - ./console.sh doctrine:migrations:migrate --env=test --no-interaction @@ -12,8 +12,8 @@ before_script: test:php-5.6: stage: test - script: phpunit --testdox + script: phpunit test:php-7: stage: test image: chill/ci-image:php-7 - script: phpunit --testdox + script: phpunit From 8849d8bb5424ba5ee1ddca0e0fe80c2cc4658c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 29 Dec 2015 17:54:00 +0100 Subject: [PATCH 14/15] fix test for custom field helper The test was requesting an entity not present in database --- Tests/Service/CustomFieldsHelperTest.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Tests/Service/CustomFieldsHelperTest.php b/Tests/Service/CustomFieldsHelperTest.php index 82092bf1c..fc7276cf7 100644 --- a/Tests/Service/CustomFieldsHelperTest.php +++ b/Tests/Service/CustomFieldsHelperTest.php @@ -52,11 +52,13 @@ class CustomFieldsHelperTest extends KernelTestCase $this->cfHelper = $container->get('chill.custom_field.helper'); - $cfieldsText = $container->get('doctrine.orm.entity_manager') - ->getRepository('ChillCustomFieldsBundle:CustomField') - ->findBy(array('type' => 'text')) - ; - $this->randomCFText = end($cfieldsText); + $this->randomCFText = (new CustomField()) + ->setSlug('my-slug') + ->setActive(true) + ->setName(array('fr' => 'my cf')) + ->setOptions(array('maxLength' => 1000)) + ->setType('text') + ; } public function testRenderCustomField() From f145ae7f88e8ae9f723fcb5ca9c899745a0063a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 30 Dec 2015 16:57:16 +0100 Subject: [PATCH 15/15] re-introduce post_text extension in fields.html.twig + tests --- Resources/views/Form/fields.html.twig | 24 ++++++++ .../PostTextIntegerExtensionTest.php | 58 +++++++++++++++++++ .../Extension/PostTextNumberExtensionTest.php | 58 +++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 Tests/Form/Extension/PostTextIntegerExtensionTest.php create mode 100644 Tests/Form/Extension/PostTextNumberExtensionTest.php diff --git a/Resources/views/Form/fields.html.twig b/Resources/views/Form/fields.html.twig index 990f9719f..56f5b8733 100644 --- a/Resources/views/Form/fields.html.twig +++ b/Resources/views/Form/fields.html.twig @@ -80,5 +80,29 @@ {% endblock cf_choices_row %} +{# extend the number type to add post_text extension #} +{% block number_widget %} +{%- if post_text is defined and post_text is not empty-%} +
+{%- endif -%} +{{ block('form_widget') }} +{%- if post_text is defined and post_text is not empty-%} +{{ post_text }} +
+{%- endif -%} +{% endblock %} + +{# extend the number type to add post_text extension #} +{% block integer_widget %} +{%- if post_text is defined and post_text is not empty-%} +
+{%- endif -%} +{{ block('form_widget') }} +{%- if post_text is defined and post_text is not empty-%} +{{ post_text }} +
+{%- endif -%} +{% endblock %} + {# The choice_with_other_widget widget is defined in the main bundle #} diff --git a/Tests/Form/Extension/PostTextIntegerExtensionTest.php b/Tests/Form/Extension/PostTextIntegerExtensionTest.php new file mode 100644 index 000000000..08664735a --- /dev/null +++ b/Tests/Form/Extension/PostTextIntegerExtensionTest.php @@ -0,0 +1,58 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\CustomFields\Tests\Form\Extension; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; + +/** + * Test the post-text extension + * + * @author Julien Fastré + */ +class PostTextIntegerExtensionTest extends KernelTestCase +{ + /** + * + * @var \Symfony\Component\Form\FormBuilderInterface + */ + private $formBuilder; + + public function setUp() + { + self::bootKernel(); + + $container = self::$kernel->getContainer(); + + $this->formBuilder = $container->get('form.factory') + ->createBuilder('form', null); + } + + public function testCreateView() + { + $form = $this->formBuilder->add('test', 'integer', array( + 'post_text' => 'my text' + ))->getForm(); + + $view = $form->createView(); + + $this->assertEquals('my text', $view['test']->vars['post_text']); + } + +} diff --git a/Tests/Form/Extension/PostTextNumberExtensionTest.php b/Tests/Form/Extension/PostTextNumberExtensionTest.php new file mode 100644 index 000000000..4348445d9 --- /dev/null +++ b/Tests/Form/Extension/PostTextNumberExtensionTest.php @@ -0,0 +1,58 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\CustomFields\Tests\Form\Extension; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; + +/** + * Test the post-text extension + * + * @author Julien Fastré + */ +class PostTextNumberExtensionTest extends KernelTestCase +{ + /** + * + * @var \Symfony\Component\Form\FormBuilderInterface + */ + private $formBuilder; + + public function setUp() + { + self::bootKernel(); + + $container = self::$kernel->getContainer(); + + $this->formBuilder = $container->get('form.factory') + ->createBuilder('form', null); + } + + public function testCreateView() + { + $form = $this->formBuilder->add('test', 'number', array( + 'post_text' => 'my text' + ))->getForm(); + + $view = $form->createView(); + + $this->assertEquals('my text', $view['test']->vars['post_text']); + } + +}