From c3390bdfc3910b0601e1a4670e179b2f0214aeb4 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Sun, 8 Jul 2018 22:47:11 +1000 Subject: [PATCH] Added more steps + fixed Date trait. --- Makefile | 4 +--- src/D7/EmailTrait.php | 9 ++++----- src/D7/FileDownloadTrait.php | 13 ++++++------- src/D7/FileTrait.php | 25 ++++++++++++++++--------- src/D7/OverrideTrait.php | 5 ++++- src/D7/TaxonomyTrait.php | 28 ++++++++++++++++++++++++++++ src/D7/UserTrait.php | 7 +++---- src/D7/VariableTrait.php | 7 +++---- src/D7/WatchdogTrait.php | 3 +-- src/D8/ContentTrait.php | 3 +-- src/D8/TaxonomyTrait.php | 7 +++---- src/D8/WatchdogTrait.php | 3 +-- src/DateTrait.php | 2 +- src/FieldTrait.php | 7 +++---- src/LinkTrait.php | 4 +--- src/PathTrait.php | 6 ++---- src/ResponseTrait.php | 6 ++---- 17 files changed, 80 insertions(+), 59 deletions(-) create mode 100644 src/D7/TaxonomyTrait.php diff --git a/Makefile b/Makefile index 2e088ed4..96d2c549 100644 --- a/Makefile +++ b/Makefile @@ -32,10 +32,8 @@ build: ## Remove dependencies. clean: $(call title,Removing dependencies) - $(call exec,chmod -Rf 777 $(WEBROOT)/sites/default) + $(call exec,chmod -Rf 777 $(WEBROOT)/sites/default||true) $(call exec,git ls-files --directory --other -i --exclude-from=.gitignore $(WEBROOT)|xargs rm -Rf) - $(call exec,rm -Rf vendor) - $(call exec,rm -Rf node_modules) ## Remove dependencies and Docker images. clean-full: docker-stop docker-destroy clean diff --git a/src/D7/EmailTrait.php b/src/D7/EmailTrait.php index a7a1c09c..5333247f 100644 --- a/src/D7/EmailTrait.php +++ b/src/D7/EmailTrait.php @@ -5,7 +5,6 @@ use Behat\Behat\Hook\Scope\AfterScenarioScope; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\PyStringNode; -use Behat\Mink\Exception\ExpectationException; /** * Trait EmailTrait. @@ -82,7 +81,7 @@ public function emailAssertEmailIsSentTo($address) { } } - throw new ExpectationException(sprintf("Unable to find email sent to '%s' retrieved from test email collector.", $address), $this->getSession()); + throw new \Exception(sprintf("Unable to find email sent to '%s' retrieved from test email collector.", $address)); } /** @@ -90,7 +89,7 @@ public function emailAssertEmailIsSentTo($address) { */ public function emailAssertNoEmailsWereSent() { if (count($this->emailGetCollectedEmails()) > 0) { - throw new ExpectationException('No emails were supposed to be sent', $this->getSession()); + throw new \Exception('No emails were supposed to be sent'); } } @@ -132,7 +131,7 @@ public function emailAssertEmailContains($field, PyStringNode $string, $exact = } } - throw new ExpectationException(sprintf("Unable to find email with%s text '%s' in field '%s' retrieved from test email collector.", ($exact ? ' exact' : ''), $string, $field), $this->getSession()); + throw new \Exception(sprintf("Unable to find email with%s text '%s' in field '%s' retrieved from test email collector.", ($exact ? ' exact' : ''), $string, $field)); } /** @@ -155,7 +154,7 @@ public function emailAssertEmailNotContains($field, PyStringNode $string, $exact foreach ($this->emailGetCollectedEmails() as $email) { $field_string = $exact ? $email[$field] : trim(preg_replace('/\s+/', ' ', $email[$field]));; if (strpos($field_string, $string) !== FALSE) { - throw new ExpectationException(sprintf("Found email with%s text '%s' in field '%s' retrieved from test email collector, but should not.", ($exact ? ' exact' : ''), $string, $field), $this->getSession()); + throw new \Exception(sprintf("Found email with%s text '%s' in field '%s' retrieved from test email collector, but should not.", ($exact ? ' exact' : ''), $string, $field)); } } } diff --git a/src/D7/FileDownloadTrait.php b/src/D7/FileDownloadTrait.php index 8b2a5824..1f6d3938 100644 --- a/src/D7/FileDownloadTrait.php +++ b/src/D7/FileDownloadTrait.php @@ -6,7 +6,6 @@ use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; -use Behat\Mink\Exception\ExpectationException; use Symfony\Component\Filesystem\Filesystem; /** @@ -91,10 +90,10 @@ public function fileDownloadAssertLinkPresence($link, $presense) { $link_element = $page->findLink($link); if ($should_be_present && !$link_element) { - throw new ExpectationException(sprintf('No link "%s" is present on the page, but expected to be present', $link)); + throw new \Exception(sprintf('No link "%s" is present on the page, but expected to be present', $link)); } elseif (!$should_be_present && $link_element) { - throw new ExpectationException(sprintf('Link "%s" is present on the page, but expected to be absent', $link)); + throw new \Exception(sprintf('Link "%s" is present on the page, but expected to be absent', $link)); } return $link_element; @@ -120,7 +119,7 @@ public function fileDownloadAssertFileContains(PyStringNode $string) { } } - throw new ExpectationException('Unable to find a content line with searched string.'); + throw new \Exception('Unable to find a content line with searched string.'); } /** @@ -132,7 +131,7 @@ public function fileDownloadAssertFileName($name) { } if ($name != $this->fileDownloadDownloadedFileInfo['file_name']) { - throw new ExpectationException(sprintf('Downloaded file %s, but expected %s', $this->fileDownloadDownloadedFileInfo['file_name'], $name)); + throw new \Exception(sprintf('Downloaded file %s, but expected %s', $this->fileDownloadDownloadedFileInfo['file_name'], $name)); } } @@ -150,7 +149,7 @@ public function fileDownloadAssertZipContains(TableNode $files) { } if (!empty($errors)) { - throw new ExpectationException(implode(PHP_EOL, $errors)); + throw new \Exception(implode(PHP_EOL, $errors)); } } @@ -168,7 +167,7 @@ public function fileDownloadAssertNoZipContains(TableNode $files) { } if (!empty($errors)) { - throw new ExpectationException(implode(PHP_EOL, $errors)); + throw new \Exception(implode(PHP_EOL, $errors)); } } diff --git a/src/D7/FileTrait.php b/src/D7/FileTrait.php index d2dfd453..6d1d5101 100644 --- a/src/D7/FileTrait.php +++ b/src/D7/FileTrait.php @@ -12,8 +12,6 @@ trait FileTrait { /** - * Create managed file from existing local file. - * * @Given managed file: */ public function fileCreateManaged(TableNode $nodesTable) { @@ -23,25 +21,34 @@ public function fileCreateManaged(TableNode $nodesTable) { if (empty($node->path)) { throw new \RuntimeException('"path" property is required'); } + $path = ltrim($node->path, '/'); // Limited support for remote files: all remote files are considered // oembed objects and therefore only oembed'able objects will be saved. if (parse_url($node->path, PHP_URL_SCHEME) !== NULL) { - $provider = media_internet_get_provider($node->path); + $provider = media_internet_get_provider($path); $file = $provider->save(); } // Local file. else { - $file_path = dirname(dirname(__FILE__)) . '/' . ltrim($node->path, '/'); - $destination = 'public://' . basename($file_path); - if (!is_readable($file_path)) { - throw new \RuntimeException('Unable to find file ' . $file_path); + // Get fixture file path. + if ($this->getMinkParameter('files_path')) { + $full_path = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path; + if (is_file($full_path)) { + $path = $full_path; + } } - $file = file_save_data(file_get_contents($file_path), $destination, FILE_EXISTS_REPLACE); + + if (!is_readable($path)) { + throw new \RuntimeException('Unable to find file ' . $path); + } + + $destination = 'public://' . basename($path); + $file = file_save_data(file_get_contents($path), $destination, FILE_EXISTS_REPLACE); } if (!$file) { - throw new \RuntimeException('Unable to save managed file ' . $file_path); + throw new \RuntimeException('Unable to save managed file ' . $path); } } } diff --git a/src/D7/OverrideTrait.php b/src/D7/OverrideTrait.php index 8feced49..add5e083 100644 --- a/src/D7/OverrideTrait.php +++ b/src/D7/OverrideTrait.php @@ -12,7 +12,10 @@ trait OverrideTrait { /** - * {@inheritdoc} + * Creates and authenticates a user with the given role(s). + * + * @Given I am logged in as a user with the :role role(s) + * @Given I am logged in as a/an :role */ public function assertAuthenticatedByRole($role) { // Override parent assertion to allow using 'anonymous user' role without diff --git a/src/D7/TaxonomyTrait.php b/src/D7/TaxonomyTrait.php new file mode 100644 index 00000000..9b5e89b7 --- /dev/null +++ b/src/D7/TaxonomyTrait.php @@ -0,0 +1,28 @@ + $name, + 'vid' => $vocab->vid, + ]); + + if (count($found) == 0) { + throw new \Exception(printf('Taxonomy term "%s" from vocabulary "%s" does not exist', $name, $vocabulary)); + } + } + +} diff --git a/src/D7/UserTrait.php b/src/D7/UserTrait.php index 91279e5d..91bfb2a5 100644 --- a/src/D7/UserTrait.php +++ b/src/D7/UserTrait.php @@ -3,7 +3,6 @@ namespace IntegratedExperts\BehatSteps\D7; use Behat\Gherkin\Node\TableNode; -use Behat\Mink\Exception\ExpectationException; /** * Trait UserTrait. @@ -58,7 +57,7 @@ public function userAssertHasRoles($name, $roles) { }, $roles); if (count(array_intersect($roles, $user->roles)) != count($roles)) { - throw new ExpectationException(sprintf('User "%s" does not have role(s) "%s", but has roles "%s"', $name, implode('", "', $roles), implode('", "', $user->roles))); + throw new \Exception(sprintf('User "%s" does not have role(s) "%s", but has roles "%s"', $name, implode('", "', $roles), implode('", "', $user->roles))); } } @@ -74,7 +73,7 @@ public function userAssertHasNoRoles($name, $roles) { }, $roles); if (count(array_intersect($roles, $user->roles)) > 0) { - throw new ExpectationException(sprintf('User "%s" should not have roles(s) "%s", but has "%s"', $name, implode('", "', $roles), implode('", "', $user->roles))); + throw new \Exception(sprintf('User "%s" should not have roles(s) "%s", but has "%s"', $name, implode('", "', $roles), implode('", "', $user->roles))); } } @@ -90,7 +89,7 @@ public function userAssertHasStatus($name, $status) { } if ($user->status != $status) { - throw new ExpectationException(sprintf('User "%s" is expected to have status "%s", but has status "%s"', $name, $status ? 'active' : 'blocked', $user->status ? 'active' : 'blocked')); + throw new \Exception(sprintf('User "%s" is expected to have status "%s", but has status "%s"', $name, $status ? 'active' : 'blocked', $user->status ? 'active' : 'blocked')); } } diff --git a/src/D7/VariableTrait.php b/src/D7/VariableTrait.php index 06eb5a9c..035cf337 100644 --- a/src/D7/VariableTrait.php +++ b/src/D7/VariableTrait.php @@ -4,7 +4,6 @@ use Behat\Behat\Hook\Scope\AfterScenarioScope; use Behat\Behat\Hook\Scope\BeforeScenarioScope; -use Behat\Mink\Exception\ExpectationException; /** * Class VariableTrait. @@ -36,7 +35,7 @@ public function variableAssertValue($name, $value) { $this->variableRefresh(); $variable_value = variable_get($name); if ($value != $variable_value) { - throw new ExpectationException(sprintf('Variable %s has value "%s", but should have value "%s".', $name, $variable_value, $value)); + throw new \Exception(sprintf('Variable %s has value "%s", but should have value "%s".', $name, $variable_value, $value)); } } @@ -47,7 +46,7 @@ public function variableAssertNoValue($name, $value) { $this->variableRefresh(); $variable_value = variable_get($name); if ($value == $variable_value) { - throw new ExpectationException(sprintf('Variable %s has value "%s", but should not have it.', $name, $variable_value)); + throw new \Exception(sprintf('Variable %s has value "%s", but should not have it.', $name, $variable_value)); } } @@ -58,7 +57,7 @@ public function variableAssertNullValue($name) { $this->variableRefresh(); $variable_value = variable_get($name); if (!is_null($variable_value)) { - throw new ExpectationException(sprintf('Variable %s has value "%s", but should not have any value set.', $name, $variable_value)); + throw new \Exception(sprintf('Variable %s has value "%s", but should not have any value set.', $name, $variable_value)); } } diff --git a/src/D7/WatchdogTrait.php b/src/D7/WatchdogTrait.php index b685b319..d50b2c07 100644 --- a/src/D7/WatchdogTrait.php +++ b/src/D7/WatchdogTrait.php @@ -8,7 +8,6 @@ */ use Behat\Behat\Hook\Scope\AfterScenarioScope; -use Behat\Mink\Exception\ExpectationException; /** * Trait WatchdogTrait. @@ -68,7 +67,7 @@ public function checkWatchdog(AfterScenarioScope $scope) { } if (!empty($entries)) { - throw new ExpectationException('PHP errors were logged to watchdog during this scenario.'); + throw new \Exception('PHP errors were logged to watchdog during this scenario.'); } } } diff --git a/src/D8/ContentTrait.php b/src/D8/ContentTrait.php index 6dc5a066..610df1d8 100644 --- a/src/D8/ContentTrait.php +++ b/src/D8/ContentTrait.php @@ -4,7 +4,6 @@ use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\TableNode; -use Behat\Mink\Exception\ExpectationException; /** * Trait ContentTrait. @@ -112,7 +111,7 @@ public function contentModeratePageWithTitle($type, $title, $old_state, $new_sta $node = current($nodes); $current_old_state = $node->get('moderation_state')->first()->getString(); if ($current_old_state != $old_state) { - throw new ExpectationException(sprintf('The current state "%s" is different from "%s"', $current_old_state, $old_state)); + throw new \Exception(sprintf('The current state "%s" is different from "%s"', $current_old_state, $old_state)); } $node->set('moderation_state', $new_state); diff --git a/src/D8/TaxonomyTrait.php b/src/D8/TaxonomyTrait.php index 558ea441..980fc28c 100644 --- a/src/D8/TaxonomyTrait.php +++ b/src/D8/TaxonomyTrait.php @@ -3,7 +3,6 @@ namespace IntegratedExperts\BehatSteps\D8; use Behat\Gherkin\Node\TableNode; -use Behat\Mink\Exception\ExpectationException; use Drupal\taxonomy\Entity\Vocabulary; /** @@ -19,10 +18,10 @@ trait TaxonomyTrait { public function taxonomyAssertVocabularyExist($name, $vid) { $vocab = Vocabulary::load($vid); if (!$vocab) { - throw new ExpectationException(sprintf('"%s" vocabulary does not exist', $vid)); + throw new \Exception(sprintf('"%s" vocabulary does not exist', $vid)); } elseif ($vocab->get('name') != $name) { - throw new ExpectationException(sprintf('"%s" vocabulary name is not "%s"', $vid, $name)); + throw new \Exception(sprintf('"%s" vocabulary name is not "%s"', $vid, $name)); } } @@ -44,7 +43,7 @@ public function taxonomyAssertTermExistsByName($name, $vid) { ]); if (count($found) == 0) { - throw new ExpectationException(sprintf('Taxonomy term "%s" from vocabulary "%s" does not exist', $name, $vid)); + throw new \Exception(sprintf('Taxonomy term "%s" from vocabulary "%s" does not exist', $name, $vid)); } } diff --git a/src/D8/WatchdogTrait.php b/src/D8/WatchdogTrait.php index fe6b1ff5..88bae328 100644 --- a/src/D8/WatchdogTrait.php +++ b/src/D8/WatchdogTrait.php @@ -8,7 +8,6 @@ */ use Behat\Behat\Hook\Scope\AfterScenarioScope; -use Behat\Mink\Exception\ExpectationException; /** * Trait WatchdogTrait. @@ -68,7 +67,7 @@ public function checkWatchdog(AfterScenarioScope $scope) { } if (!empty($entries)) { - throw new ExpectationException('PHP errors were logged to watchdog during this scenario.'); + throw new \Exception('PHP errors were logged to watchdog during this scenario.'); } } } diff --git a/src/DateTrait.php b/src/DateTrait.php index 9a7d5ca1..8ffafd90 100644 --- a/src/DateTrait.php +++ b/src/DateTrait.php @@ -30,7 +30,7 @@ trait DateTrait { public static function dateProcess($value, $now = NULL) { // If `now` is not provided, round to the current hour to make sure that // assertions are running within the same timeframe (for long tests). - $now = $now ? $now : strtotime(date('Y-m-d H:m:00', time())); + $now = $now ? $now : strtotime(date('Y-m-d 12:30:00', time())); return preg_replace_callback('/\[([^:]+):([^\]\[\#]+)(?:\#([^\]\[]+))?\]/', function ($matches) use ($now) { $timestamp = strtotime($matches[2], $now); diff --git a/src/FieldTrait.php b/src/FieldTrait.php index 522affc1..03a462fd 100644 --- a/src/FieldTrait.php +++ b/src/FieldTrait.php @@ -3,7 +3,6 @@ namespace IntegratedExperts\BehatSteps; use Behat\Mink\Exception\ElementNotFoundException; -use Behat\Mink\Exception\ExpectationException; /** * Trait Field. @@ -39,7 +38,7 @@ public function fieldAssertNotExists($field_name) { $field = $field ? $field : $page->findById($field_name); if ($field !== NULL) { - throw new ExpectationException(sprintf('A field "%s" appears on this page, but it should not.', $field_name), $this->getSession() + throw new \Exception(sprintf('A field "%s" appears on this page, but it should not.', $field_name), $this->getSession() ->getDriver()); } } @@ -63,11 +62,11 @@ public function fieldAssertState($field_name, $disabled) { $field = $this->fieldAssertExists($field_name); if ($disabled == 'disabled' && !$field->hasAttribute('disabled')) { - throw new ExpectationException(sprintf('A field "%s" should be disabled, but it is not.', $field_name), $this->getSession() + throw new \Exception(sprintf('A field "%s" should be disabled, but it is not.', $field_name), $this->getSession() ->getDriver()); } elseif ($disabled != 'disabled' && $field->hasAttribute('disabled')) { - throw new ExpectationException(sprintf('A field "%s" should not be disabled, but it is.', $field_name), $this->getSession() + throw new \Exception(sprintf('A field "%s" should not be disabled, but it is.', $field_name), $this->getSession() ->getDriver()); } } diff --git a/src/LinkTrait.php b/src/LinkTrait.php index e228ef9f..64f8b790 100644 --- a/src/LinkTrait.php +++ b/src/LinkTrait.php @@ -2,8 +2,6 @@ namespace IntegratedExperts\BehatSteps; -use Behat\Mink\Exception\ExpectationException; - /** * Trait LinkTrait. * @@ -41,7 +39,7 @@ public function linkAssertTextHref($text, $href, $locator = NULL) { // Support for simplified wildcard using '*'. $pattern = strpos($href, '*') !== FALSE ? str_replace('\*', '.*', $pattern) : $pattern; if (!preg_match($pattern, $link->getAttribute('href'))) { - throw new ExpectationException(sprintf('The link href "%s" does not match the specified href "%s"', $link->getAttribute('href'), $href)); + throw new \Exception(sprintf('The link href "%s" does not match the specified href "%s"', $link->getAttribute('href'), $href)); } } diff --git a/src/PathTrait.php b/src/PathTrait.php index d93cff33..fe9a8712 100644 --- a/src/PathTrait.php +++ b/src/PathTrait.php @@ -2,8 +2,6 @@ namespace IntegratedExperts\BehatSteps; -use Behat\Mink\Exception\ExpectationException; - /** * Trait PathTrait. * @@ -21,7 +19,7 @@ public function pathAssertCurrent($path) { $current_path = $current_path == '' ? '' : $current_path; if ($current_path != ltrim($path, '/')) { - throw new ExpectationException(sprintf('Current path is "%s", but expected is "%s"', $current_path, $path)); + throw new \Exception(sprintf('Current path is "%s", but expected is "%s"', $current_path, $path)); } } @@ -34,7 +32,7 @@ public function pathAssertNotCurrent($path) { $current_path = ltrim($current_path, '/'); if ($current_path == $path) { - throw new ExpectationException(sprintf('Current path should not be "%s"', $current_path)); + throw new \Exception(sprintf('Current path should not be "%s"', $current_path)); } return TRUE; diff --git a/src/ResponseTrait.php b/src/ResponseTrait.php index 0a915a25..e5388e34 100644 --- a/src/ResponseTrait.php +++ b/src/ResponseTrait.php @@ -2,8 +2,6 @@ namespace IntegratedExperts\BehatSteps; -use Behat\Mink\Exception\ExpectationException; - /** * Trait ResponseTrait. * @@ -18,7 +16,7 @@ public function responseAssertContainsHeader($name) { $header = $this->getSession()->getResponseHeader($name); if (!$header) { - throw new ExpectationException(sprintf('Response does not contain header %s', $name), $this->getSession()->getDriver()); + throw new \Exception(sprintf('Response does not contain header %s', $name), $this->getSession()->getDriver()); } } @@ -29,7 +27,7 @@ public function responseAssertNotContainsHeader($name) { $header = $this->getSession()->getResponseHeader($name); if ($header) { - throw new ExpectationException(sprintf('Response contains header %s, but should not', $name), $this->getSession()->getDriver()); + throw new \Exception(sprintf('Response contains header %s, but should not', $name), $this->getSession()->getDriver()); } }