Source of file FixtureContext.php
Size: 8,540 Bytes - Last Modified: 2021-12-23T10:28:10+00:00
/var/www/docs.ssmods.com/process/src/tests/behat/src/FixtureContext.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 | <?php namespace SilverStripe\CampaignAdmin\Tests\Behat\Context; use Behat\Mink\Element\DocumentElement; use Behat\Mink\Element\NodeElement; use PHPUnit\Framework\Assert; use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext; use SilverStripe\BehatExtension\Utility\StepHelper; use SilverStripe\MinkFacebookWebDriver\FacebookWebDriver; use SilverStripe\Versioned\ChangeSet; /** * Context used to create fixtures in the SilverStripe ORM. */ class FixtureContext extends BaseFixtureContext { use StepHelper; /** * @Then /^I should see the campaign "([^"]+)"$/ * @param string $name */ public function iShouldSeeTheCampaign($name) { $item = $this->getCampaign($name); Assert::assertNotNull($item, "Could not find campaign \"$name\" in the list"); } /** * @Then /^I should not see the campaign "([^"]+)"$/ * @param string $name */ public function iShouldNotSeeTheCampaign($name) { $item = $this->getCampaign($name); Assert::assertNull($item, "Found campaign \"$name\" in the list"); } /** * @Then /^I should see "([^"]+)" in column (\d+) of the "([^"]+)" campaign$/ * @param string $text * @param string $column * @param string $name */ public function iShouldSeeInTheCampaignColumn($text, $column, $name) { $campaignRow = $this->getCampaign($name); Assert::assertNotNull($campaignRow, sprintf('Could not find campaign "%s"', $name)); $cell = $campaignRow->find('xpath', "/td[{$column}][contains(text(), '{$text}')]"); Assert::assertNotNull($cell, sprintf('Could not find "%s" in column %d of "%s"', $text, $column, $name)); } /** * @Given /^I view the campaign "([^"]*)"$/ * @param $name */ public function iViewTheCampaign($name) { $item = $this->getCampaign($name); Assert::assertNotNull($item, sprintf('Campaign %s not found', $name)); $item->find('css', 'td')->click(); } /** * @Given /^I edit the campaign "([^"]*)"$/ * @param $name */ public function iEditTheCampaign($name) { $item = $this->getCampaign($name); Assert::assertNotNull($item, sprintf('Campaign %s not found', $name)); $button = $item->find('css', '.font-icon-cog'); Assert::assertNotNull($button, sprintf('Campaign %s has no edit button', $name)); $button->click(); } /** * @Given /^I delete the campaign "([^"]*)"$/ * @param $name */ public function iDeleteTheCampaign($name) { $item = $this->getCampaign($name); Assert::assertNotNull($item, sprintf('Campaign %s not found', $name)); $button = $item->find('css', '.font-icon-cancel'); Assert::assertNotNull($button, sprintf('Campaign %s has no delete button', $name)); $button->click(); /** @var FacebookWebDriver $driver */ $driver = $this->getMainContext()->getSession()->getDriver(); $webDriver = $driver->getWebDriver(); $webDriver->switchTo()->alert()->accept(); } /** * @Then /^I should see the "([^"]*)" form$/ * @param string $id HTML ID of form */ public function iShouldSeeTheForm($id) { /** @var DocumentElement $page */ $page = $this->getMainContext()->getSession()->getPage(); $form = $page->find('css', "form#{$id}"); Assert::assertNotNull($form, "form with id $id could not be found"); Assert::assertTrue($form->isVisible(), "form with id $id is not visible"); } /** * Checks that the message box contains specified text. * * @Then /^I should see "(?P<text>(?:[^"]|\\")*)" in the message box$/ * @param string $text */ public function assertMessageBoxContainsText($text) { /** @var FeatureContext $mainContext */ $mainContext = $this->getMainContext(); $mainContext ->assertSession() ->elementTextContains('css', '.message-box', str_replace('\\"', '"', $text)); } /** * @Then I should see a modal titled :title * @param string $title */ public function iShouldSeeAModalTitled($title) { $page = $this->getMainContext()->getSession()->getPage(); $modalTitle = $page->find('css', '[role=dialog] .modal-header > .modal-title'); Assert::assertNotNull($modalTitle, 'No modal on the page'); Assert::assertTrue($modalTitle->getText() == $title); } /** * @Then /^I close the modal$/ */ public function iCloseTheModal() { /** @var DocumentElement $page */ $page = $this->getMainContext()->getSession()->getPage(); $button = $page->find('css', '.modal-header .close'); Assert::assertNotNull($button, 'Close button not found'); $button->click(); } /** * @Given /^I click the "([^"]+)" gallery item$/ * @param $name */ public function iClickTheGalleryItem($name) { /** @var DocumentElement $page */ $page = $this->getMainContext()->getSession()->getPage(); // Find by cell $cell = $page->find( 'xpath', "//div[contains(@class, 'gallery-item')]//div[contains(text(), '{$name}')]" ); Assert::assertNotNull($cell, sprintf('Gallery item "%s" could not be found', $name)); $cell->click(); } /** * @Given /^I should( not? |\s*)see the "([^"]+)" campaign item$/ * @param $negate boolean * @param $name string */ public function iSeeTheCampaignItem($negate, $name) { $item = $this->getCampaignItem($name); $shouldSee = !trim($negate); if ($shouldSee) { Assert::assertNotNull($item, sprintf('Item "%s" could not be found', $name)); Assert::assertTrue($item->isVisible(), sprintf('Item "%s" is not visible', $name)); } elseif ($item) { Assert::assertFalse($item->isVisible(), sprintf('Item "%s" is visible', $name)); } else { Assert::assertNull($item, sprintf('Item "%s" exists', $name)); } } /** * @Given /^I select the "([^"]+)" campaign item$/ * @param $name */ public function iSelectTheCampaignItem($name) { $item = $this->getCampaignItem($name); Assert::assertNotNull($item, sprintf('Item "%s" could not be found', $name)); $item->click(); } /** * @Given /^a campaign "([^"]+)" with changes (".*)$/ * @param $id * @param $data */ public function stepCreateCampaignWithItems($id, $data) { $class = $this->convertTypeToClass(ChangeSet::class); /* @var ChangeSet $campaign */ $campaign = $this->getFixtureFactory()->get($class, $id); if (!$campaign) { $campaign = $this->getFixtureFactory()->createObject($class, $id); } preg_match_all( '/"(?<content_class>[^"]+)"\s*=\s*"(?<identifier>[^"]+)"/', $data, $matches ); $itemMap = array_combine($matches['content_class'], $matches['identifier']); foreach ($itemMap as $contentClass => $identifier) { $class = $this->convertTypeToClass($contentClass); $record = $this->getFixtureFactory()->get($class, $identifier); if (!$record) { $record = $this->getFixtureFactory()->createObject($class, $identifier); } $campaign->addObject($record); } } /** * Helper for finding items in the visible campaign view * * @param string $name Title of item * @return NodeElement */ protected function getCampaign($name) { /** @var DocumentElement $page */ $page = $this->getMainContext()->getSession()->getPage(); // Find by row $row = $page->find( 'xpath', "//tr[contains(@class, 'grid-field__row')]//td[contains(text(), '{$name}')]/.." ); return $row ?: null; } /** * Gets a change set item in the detail view * @param $name * @return NodeElement */ protected function getCampaignItem($name) { /** @var DocumentElement $page */ $page = $this->getMainContext()->getSession()->getPage(); return $page->find( 'xpath', "//h4[contains(@class, 'list-group-item__heading') and contains(text(), '{$name}')]" ); } } |