Skip to content

Commit

Permalink
Added more tests and fixed tratis. (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Skrypnyk authored Dec 8, 2020
1 parent 84c0e16 commit 9181780
Show file tree
Hide file tree
Showing 69 changed files with 3,941 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/D8/EmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public function emailAssertEmailContains($field, PyStringNode $string, $exact =
$string = strval($string);
$string = $exact ? $string : trim(preg_replace('/\s+/', ' ', $string));
foreach (self::emailGetCollectedEmails() as $email) {
$field_string = $exact ? $email[$field] : trim(preg_replace('/\s+/', ' ', $email[$field]));
$field_string = $email['params'][$field] ?? $email[$field];
$field_string = $exact ? $field_string : trim(preg_replace('/\s+/', ' ', $field_string));
if (strpos($field_string, $string) !== FALSE) {
return $email;
}
Expand Down Expand Up @@ -186,7 +187,7 @@ public function emailAssertEmailNotContainsExact($field, PyStringNode $string) {
*/
public function emailFollowLinkNumber($number, PyStringNode $subject) {
$email = $this->emailAssertEmailContains('subject', $subject);
$links = self::emailExtractLinks($email['body']);
$links = self::emailExtractLinks($email['params']['body'] ?? $email['body']);
if (empty($links)) {
throw new \Exception(sprintf('No links were found in the email with subject %s', $subject));
}
Expand Down
4 changes: 2 additions & 2 deletions src/D8/FileDownloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function fileDownloadAssertNoZipContains(TableNode $files) {
* Open downloaded ZIP archive and validate contents.
*/
protected function fileDownloadOpenZip() {
if (!class_exists('ZipArchive')) {
if (!class_exists('\ZipArchive')) {
throw new \RuntimeException('ZIP extension is not enabled for PHP');
}

Expand All @@ -213,7 +213,7 @@ protected function fileDownloadOpenZip() {
throw new \Exception('Downloaded file does not have correct headers set for ZIP.');
}

$zip = new ZipArchive();
$zip = new \ZipArchive();
$result = $zip->open($this->fileDownloadDownloadedFileInfo['file_path']);
if ($result !== TRUE) {
if ($result == ZipArchive::ER_NOZIP) {
Expand Down
2 changes: 1 addition & 1 deletion src/D8/MediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function mediaCreateEntity($stub) {
throw new \Exception("Cannot create media because it is missing the required property 'bundle'.");
}

$bundles = \Drupal::entityTypeManager()->getBundleInfo('media');
$bundles = \Drupal::getContainer()->get('entity_type.bundle.info')->getBundleInfo('media');
if (!in_array($stub->bundle, array_keys($bundles))) {
throw new \Exception("Cannot create media because provided bundle '$stub->bundle' does not exist.");
}
Expand Down
6 changes: 0 additions & 6 deletions src/D8/MenuTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ trait MenuTrait {
/**
* Remove menu.
*
* @code
* @Given no menus:
* | main_menu |
* | footer_menu |
* @endcode
*
* @Given no menus:
*/
public function menuDelete(TableNode $table) {
Expand Down
21 changes: 17 additions & 4 deletions src/D8/OverrideTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
trait OverrideTrait {

/**
* {@inheritdoc}
* Creates one or more terms on an existing vocabulary.
*
* Provide term data in the following format:
*
* | name | parent | description | weight | taxonomy_field_image |
* | Snook | Fish | Marine fish | 10 | snook-123.jpg |
* | ... | ... | ... | ... | ... |
*
* Only the 'name' field is required.
*
* @Given :vocabulary terms:
*/
public function createTerms($vocabulary, TableNode $table) {
// Delete entities before creating them.
Expand All @@ -23,7 +33,7 @@ public function createTerms($vocabulary, TableNode $table) {
}

/**
* {@inheritdoc}
* @Given :type content:
*/
public function createNodes($type, TableNode $table) {
$filtered_table = TableNode::fromList($table->getColumn(0));
Expand All @@ -33,7 +43,7 @@ public function createNodes($type, TableNode $table) {
}

/**
* {@inheritdoc}
* @Given users:
*/
public function createUsers(TableNode $table) {
// Delete entities before creating them.
Expand All @@ -42,7 +52,10 @@ public function createUsers(TableNode $table) {
}

/**
* {@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
Expand Down
13 changes: 9 additions & 4 deletions src/D8/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ public function userVisitOwnProfilePage() {
public function userDelete(TableNode $usersTable) {
foreach ($usersTable->getHash() as $userHash) {
$user = NULL;
if (isset($userHash['mail'])) {
$user = $this->userGetByMail($userHash['mail']);
try {
if (isset($userHash['mail'])) {
$user = $this->userGetByMail($userHash['mail']);
}
elseif (isset($userHash['name'])) {
$user = $this->userGetByMail($userHash['name']);
}
}
elseif (isset($userHash['name'])) {
$user = $this->userGetByMail($userHash['name']);
catch (\Exception $exception) {
// User may not exist - do nothing.
}

if ($user) {
Expand Down
83 changes: 83 additions & 0 deletions tests/behat/bootstrap/FeatureContextD8.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,29 @@
*/

use Behat\Behat\Hook\Scope\AfterFeatureScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Mink\Driver\Selenium2Driver;
use Drupal\Core\Extension\MissingDependencyException;
use Drupal\DrupalExtension\Context\DrupalContext;
use Drupal\file\Entity\File;
use Drupal\user\Entity\User;
use IntegratedExperts\BehatSteps\D8\BigPipeTrait;
use IntegratedExperts\BehatSteps\D8\ContentTrait;
use IntegratedExperts\BehatSteps\D8\DraggableViewsTrait;
use IntegratedExperts\BehatSteps\D8\EmailTrait;
use IntegratedExperts\BehatSteps\D8\FileDownloadTrait;
use IntegratedExperts\BehatSteps\D8\FileTrait;
use IntegratedExperts\BehatSteps\D8\MediaTrait;
use IntegratedExperts\BehatSteps\D8\MenuTrait;
use IntegratedExperts\BehatSteps\D8\OverrideTrait;
use IntegratedExperts\BehatSteps\D8\ParagraphsTrait;
use IntegratedExperts\BehatSteps\D8\RoleTrait;
use IntegratedExperts\BehatSteps\D8\TaxonomyTrait;
use IntegratedExperts\BehatSteps\D8\TestmodeTrait;
use IntegratedExperts\BehatSteps\D8\UserTrait;
use IntegratedExperts\BehatSteps\D8\WatchdogTrait;
use IntegratedExperts\BehatSteps\D8\WebformTrait;
use IntegratedExperts\BehatSteps\D8\WysiwygTrait;
use IntegratedExperts\BehatSteps\FieldTrait;
use IntegratedExperts\BehatSteps\LinkTrait;
use IntegratedExperts\BehatSteps\PathTrait;
Expand All @@ -31,12 +47,25 @@ class FeatureContextD8 extends DrupalContext {

use BigPipeTrait;
use ContentTrait;
use DraggableViewsTrait;
use EmailTrait;
use FieldTrait;
use FileDownloadTrait;
use FileTrait;
use LinkTrait;
use MediaTrait;
use MenuTrait;
use OverrideTrait;
use ParagraphsTrait;
use PathTrait;
use ResponseTrait;
use RoleTrait;
use TaxonomyTrait;
use TestmodeTrait;
use UserTrait;
use WatchdogTrait;
use WebformTrait;
use WysiwygTrait;

/**
* @Then user :name does not exists
Expand Down Expand Up @@ -165,4 +194,58 @@ public function uninstallModule($name) {
}
}

/**
* @When I send test email to :email with
* @When I send test email to :email with:
*/
public function sendTestEmail($email, PyStringNode $string) {
\Drupal::service('plugin.manager.mail')->mail(
'mysite_core',
'test_email',
$email,
\Drupal::languageManager()->getDefaultLanguage(),
['body' => strval($string)],
FALSE
);
}

/**
* @Then :file_name file object exists
*/
public function fileObjectExist($file_name) {
$file_name = basename($file_name);
$fids = $this->fileLoadMultiple(['filename' => $file_name]);
if (empty($fids)) {
throw new \Exception(sprintf('"%s" file does not exist in DB, but it should', $file_name));
}

$fid = reset($fids);
$file = File::load($fid);

if ($file_name !== $file->label()) {
throw new \Exception(sprintf('"%s" file does not exist in DB, but it should', $file_name));
}
}

/**
* Helper to load multiple files with specified conditions.
*
* @param array $conditions
* Conditions keyed by field names.
*
* @return array
* Array of file ids.
*/
protected function fileLoadMultiple(array $conditions = []) {
$query = \Drupal::entityQuery('file');
$query->addMetaData('account', User::load(1));
foreach ($conditions as $k => $v) {
$and = $query->andConditionGroup();
$and->condition($k, $v);
$query->condition($and);
}

return $query->execute();
}

}
142 changes: 142 additions & 0 deletions tests/behat/features/d8.email.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
@d8
Feature: Check that email assertions work for D8

@api
Scenario: As a developer, I want to know that email step definitions work as
expected.
# @note: No @email tag on scenario to test "Given I enable the test email system" step.
Given I enable the test email system
When I send test email to "[email protected]" with
"""
Line one of the test email content
Line two of the test email content
Line three with tabs and spaces
"""
Then an email is sent to "[email protected]"
And an email body contains:
"""
Line two of the test email content
"""
And an email body does not contain:
"""
Line four of the test email content
"""
And an email body contains exact:
"""
Line three with tabs and spaces
"""
And an email body does not contain exact:
"""
Line three with tabs and spaces
"""
But an email body contains:
"""
Line three with tabs and spaces
"""
And an email body contains:
"""
Line three with tabs and spaces
"""
And I disable the test email system

@api
Scenario: As a developer, I want to know that test email system is activated
as before and after scenario steps.
Given I enable the test email system
When I send test email to "[email protected]" with
"""
Line one of the test email content
Line two of the test email content
Line three of the test email content
"""
Then an email is sent to "[email protected]"
And an email "body" contains:
"""
Line two of the test email content
"""
And an email body does not contain:
"""
Line four of the test email content
"""
And I disable the test email system

@api
Scenario: As a developer, I want to know that test email system queue clearing
step is working.
Given I enable the test email system
When I send test email to "[email protected]" with
"""
Line one of the test email content
Line two of the test email content
Line three of the test email content
"""
Then an email is sent to "[email protected]"
And an email body contains:
"""
Line two of the test email content
"""
And an email body does not contain:
"""
Line four of the test email content
"""
When I clear the test email system queue
And an email body does not contain:
"""
Line two of the test email content
"""
And I disable the test email system

@api @email
Scenario: As a developer, I want to know that test email system is automatically
activated when @email tag is added to the scenario.
When I send test email to "[email protected]" with
"""
Line one of the test email content
Line two of the test email content
Line three of the test email content
"""
Then an email is sent to "[email protected]"
And an email body contains:
"""
Line two of the test email content
"""

@api @email
Scenario Outline: As a developer, I want to know that following a link from
the email is working.
Given I send test email to "[email protected]" with
"""
Line one of the test email content
"<content>"
Line two of the test email content
"""
Then an email is sent to "[email protected]"

And I follow the link number "<number>" in the email with the subject:
"""
Test Email
"""
Then the response status code should be 200
And I should see "Example Domain"
Examples:
| content | number |
| http://example.com | 1 |
| http://www.example.com | 1 |
| www.example.com | 1 |
| Link is a part of content http://example.com | 1 |
| http://1.example.com http://example.com http://3.example.com | 2 |
| http://1.example.com http://2.example.com http://example.com | 3 |

@api @email
Scenario: As a developer, I want to know that no emails assertions works as expected.
Given no emails were sent
Given I send test email to "[email protected]" with
"""
Line one of the test email content
"<content>"
Line two of the test email content
"""
Then an email is sent to "[email protected]"

When I clear the test email system queue
Then no emails were sent
Loading

0 comments on commit 9181780

Please sign in to comment.