diff --git a/behat.yml.dist b/behat.yml.dist index 5593324285..832185e16e 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -144,6 +144,18 @@ default: filters: tags: "@localization" + metadata: + contexts: + - Behat\MinkExtension\Context\MinkContext + - Sylius\Bundle\CoreBundle\Behat\HookContext + - Sylius\Bundle\CoreBundle\Behat\CoreContext + - Sylius\Bundle\ResourceBundle\Behat\FixtureContext + - Sylius\Bundle\WebBundle\Behat\WebContext + - Sylius\Bundle\ProductBundle\Behat\ProductContext + - Sylius\Bundle\CoreBundle\Behat\MetadataContext + filters: + tags: "@metadata" + oauth: contexts: - Behat\MinkExtension\Context\MinkContext diff --git a/src/Sylius/Bundle/CoreBundle/Behat/MetadataContext.php b/src/Sylius/Bundle/CoreBundle/Behat/MetadataContext.php new file mode 100644 index 0000000000..8fe7c97b19 --- /dev/null +++ b/src/Sylius/Bundle/CoreBundle/Behat/MetadataContext.php @@ -0,0 +1,240 @@ + + */ +class MetadataContext extends DefaultContext +{ + /** + * @When I am customizing metadata + * @When I am customizing metadata with identifier "([^"]+)" + */ + public function iAmCustomizingMetadata($identifier = 'FooBar') + { + $this->getSession()->visit($this->generateUrl('sylius_backend_metadata_customize', ['id' => $identifier])); + } + + /** + * @Then I should see metadata customization form + */ + public function iShouldSeeMetadataCustomizationForm() + { + $this->assertThereIsFormWithFields( + $this->getSession()->getPage(), + ['Title', 'Description', 'Keywords', 'Twitter Card'] + ); + } + + /** + * @Then I should see the following metadata: + */ + public function iShouldSeeTheFollowingMetadata(TableNode $metadata) + { + /** @var NodeElement $table */ + $table = $this->getSession()->getPage()->find('css', '#content > table'); + + /** @var NodeElement $row */ + $row = $table->findAll('css', 'tr')[1]; + + /** @var NodeElement[] $columns */ + $columns = $row->findAll('css', 'td'); + + $contentIndex = $this->getColumnIndex($table, 'Content'); + + /** @var NodeElement $list */ + $list = $columns[$contentIndex]; + foreach ($metadata->getRowsHash() as $key => $value) { + $currentElement = $list; + $parts = explode('.', $key); + + foreach ($parts as $part) { + $currentElement = $currentElement->find('xpath', sprintf('/ul/li[starts-with(normalize-space(.), "%s:")]', $part)); + } + + $exploded = explode(':', $currentElement->getText()); + $text = trim(end($exploded)); + + $expectedValue = $value; + if ('' === $expectedValue) { + $expectedValue = 'empty'; + } elseif (false !== strpos($expectedValue, ',')) { + $expectedValue = str_replace([', ', ','], [' ', ' '], $expectedValue); + } + + if ($text !== $expectedValue) { + throw new \Exception(sprintf( + 'Expected "%s", got "%s" (item: "%s", original value: "%s")', + $expectedValue, + $text, + $key, + $value + )); + } + } + } + + /** + * @Then I should be customizing default metadata + */ + public function iShouldBeCustomizingDefaultMetadata() + { + $this->assertItIsMetadataCustomizationPage( + $this->getSession()->getPage(), + '/DefaultPage/' + ); + } + + /** + * @Then I should be customizing products metadata + */ + public function iShouldBeCustomizingProductsMetadata() + { + $this->assertItIsMetadataCustomizationPage( + $this->getSession()->getPage(), + '/Product(?!\-)/' + ); + } + + /** + * @Then I should be customizing specific product metadata + */ + public function iShouldBeCustomizingSpecificProductMetadata() + { + $this->assertItIsMetadataCustomizationPage( + $this->getSession()->getPage(), + '/Product-\d+/' + ); + } + + /** + * @Then I should be customizing specific product variant metadata + */ + public function iShouldBeCustomizingSpecificProductVariantMetadata() + { + $this->assertItIsMetadataCustomizationPage( + $this->getSession()->getPage(), + '/ProductVariant-\d+/' + ); + } + + /** + * @Then I should see Twitter's application card form + */ + public function iShouldSeeTwitterApplicationCardForm() + { + $this->assertSession()->fieldExists('Iphone application name'); + $this->assertSession()->fieldExists('Ipad application url'); + } + + /** + * @Then I should not see Twitter's application card form + */ + public function iShouldNotSeeTwitterApplicationCardForm() + { + $this->assertSession()->fieldNotExists('Iphone application name'); + $this->assertSession()->fieldNotExists('Ipad application url'); + } + + /** + * @When /I deselect "([^"]+)"/ + */ + public function iDeselectSelectField($fieldName) + { + $this->getSession()->getPage()->selectFieldOption($fieldName, ""); + } + + /** + * @param ElementInterface $element + * @param string $regexp + * + * @throws \Exception If assertion failed + */ + private function assertItIsMetadataCustomizationPage(ElementInterface $element, $regexp) + { + if ($this->isItMetadataCustomizationPage($element, $regexp)) { + return; + } + + throw new \Exception(sprintf("It is not metadata customziation page (regexp: %s)", $regexp)); + } + + /** + * @param ElementInterface $element + * @param string $regexp + * + * @return bool + */ + private function isItMetadataCustomizationPage(ElementInterface $element, $regexp) + { + $header = $element->find('css', '.page-header h1'); + + if (false === strpos($header->getText(), "Customizing metadata")) { + return false; + } + + if (!preg_match($regexp, $header->getText())) { + return false; + } + + return true; + } + + /** + * @param ElementInterface $element + * @param array $fields + * + * @throws \Exception If assertion failed + */ + private function assertThereIsFormWithFields(ElementInterface $element, array $fields) + { + if (null !== $this->getFormWithFields($element, $fields)) { + return; + } + + throw new \Exception(sprintf("Could not found table with fields: %s", join(', ', $fields))); + } + + /** + * @param ElementInterface $element + * @param string[] $fields + * + * @return ElementInterface|null + */ + private function getFormWithFields(ElementInterface $element, array $fields) + { + /** @var NodeElement[] $forms */ + $forms = $element->findAll('css', 'form'); + + foreach ($forms as $form) { + $found = true; + foreach ($fields as $field) { + if (null === $form->findField($field)) { + $found = false; + } + } + + if ($found) { + return $form; + } + } + + return null; + } +} diff --git a/src/Sylius/Bundle/ResourceBundle/Behat/DefaultContext.php b/src/Sylius/Bundle/ResourceBundle/Behat/DefaultContext.php index 3e4fbb0e73..a5ce874ec7 100644 --- a/src/Sylius/Bundle/ResourceBundle/Behat/DefaultContext.php +++ b/src/Sylius/Bundle/ResourceBundle/Behat/DefaultContext.php @@ -46,10 +46,11 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA * @var array */ protected $actions = array( - 'viewing' => 'show', - 'creation' => 'create', - 'editing' => 'update', - 'building' => 'build', + 'viewing' => 'show', + 'creation' => 'create', + 'editing' => 'update', + 'building' => 'build', + 'customization' => 'customize', ); /** diff --git a/src/Sylius/Bundle/WebBundle/Resources/translations/messages.en.yml b/src/Sylius/Bundle/WebBundle/Resources/translations/messages.en.yml index 5f5783919d..b9e1269d69 100644 --- a/src/Sylius/Bundle/WebBundle/Resources/translations/messages.en.yml +++ b/src/Sylius/Bundle/WebBundle/Resources/translations/messages.en.yml @@ -565,7 +565,14 @@ sylius: customize: Customize metadata manage: Manage metadata content: Content - index_metadata: Metadata + index_metadata: Metadatas + default_page: + group_customize: Customize default metadata + product: + single_customize: Customize metadata + group_customize: Customize products metadata + product_variant: + single_customize: Customize metadata move_down: Move down move_up: Move up na: N/A diff --git a/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/customize.html.twig b/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/customize.html.twig index b306ba5ed0..b51588c22a 100644 --- a/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/customize.html.twig +++ b/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/customize.html.twig @@ -18,7 +18,7 @@ {% block content %} {{ form_errors(form) }} diff --git a/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/index.html.twig b/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/index.html.twig index cff881a23d..cdae7366f7 100644 --- a/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/index.html.twig +++ b/src/Sylius/Bundle/WebBundle/Resources/views/Backend/Metadata/index.html.twig @@ -15,7 +15,7 @@