Commit 37f9aef3 authored by dpi's avatar dpi

Added tests for courier_system submodule

Added phpunit.xml.dist file
Added core bootstrap resolution file.
Updated Travis configuration with details for phpunit, phpunit configuration, and setting to test courier_system group.
parent b4b4fab8
......@@ -14,7 +14,6 @@ php:
- 5.5
- 5.6
- 7
- hhvm
matrix:
fast_finish: true
......@@ -29,7 +28,7 @@ env:
# Configuration variables.
- DRUPAL_TI_MODULE_NAME="courier"
- DRUPAL_TI_SIMPLETEST_GROUP="courier"
- DRUPAL_TI_SIMPLETEST_GROUP="courier,courier_system"
# Define runners and environment vars to include before and after the
# main runners / environment vars.
......@@ -68,7 +67,7 @@ env:
- DRUPAL_TI_BEHAT_BROWSER="firefox"
# PHPUnit specific commandline arguments.
- DRUPAL_TI_PHPUNIT_ARGS=""
- DRUPAL_TI_PHPUNIT_ARGS="-c ./phpunit.xml.dist"
# Specifying the phpunit-core src/ directory is useful when e.g. a vendor/
# directory is present in the module directory, which phpunit would then
# try to find tests in. This option is relative to $TRAVIS_BUILD_DIR.
......@@ -93,7 +92,7 @@ env:
#- DRUPAL_TI_RUNNERS="phpunit simpletest behat"
# Use phpunit-core to test modules with phpunit with Drupal 8 core.
#- DRUPAL_TI_RUNNERS="phpunit-core"
- DRUPAL_TI_RUNNERS="simpletest"
- DRUPAL_TI_RUNNERS="phpunit-core simpletest"
mysql:
database: drupal_travis_db
......@@ -113,6 +112,7 @@ before_script:
- drupal-ti before_script
script:
- export SIMPLETEST_DB=mysql://root:@127.0.0.1/drupal_travis_db
- drupal-ti script
after_script:
......
<?php
namespace Drupal\Tests\courier_system\Kernel;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\courier\Entity\GlobalTemplateCollection;
use Drupal\Tests\courier\Kernel\CourierKernelTestBase;
use Drupal\user\Entity\User;
/**
* Tests Courier system.
*
* @group courier_system
*/
class CourierSystemTest extends CourierKernelTestBase {
use AssertMailTrait;
/**
* {@inheritdoc}
*/
public static $modules = ['user', 'system', 'courier_system'];
/**
* @var \Drupal\Core\Cron $cron
*/
protected $cron;
/**
* List of supported mail ID's.
* @var array
*/
protected $mailIds;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['courier', 'courier_system']);
$this->installSchema('system', ['sequences', 'queue']);
$this->installEntitySchema('courier_message_queue_item');
$this->installEntitySchema('courier_template_collection');
$this->installEntitySchema('courier_email');
$this->installEntitySchema('user');
$this->config('system.mail')
->set('interface.default', 'test_mail_collector')
->save();
$this->cron = $this->container->get('cron');
$this->mailIds = [
'register_admin_created',
'register_no_approval_required',
'register_pending_approval',
'password_reset',
'status_activated',
'status_blocked',
'cancel_confirm',
'status_canceled',
];
}
/**
* Test courier system override is off.
*/
public function testNonOverride() {
foreach ($this->mailIds as $id) {
$this->config('user.settings')
->set('notify.' . $id, TRUE)
->save();
$default_body = $this->randomMachineName();
// Override the user.module template.
$this->config('user.mail')
->set($id . '.body', $default_body)
->save();
// Turn off Courier override.
$this->config('courier_system.settings')
->set('override.user_' . $id, FALSE)
->save();
$body = $this->randomMachineName();
$this->createGlobalTemplateCollection('courier_system.user_' . $id, $body);
// Simulate.
_user_mail_notify($id, $this->createUser());
$this->cron->run();
// Email depth is two emails since some triggers will send email for user
// and admin.
$this->assertMailString('body', $default_body, 2, 'Body found in non override for ' . $id);
}
}
/**
* Test courier system override is on.
*/
public function testOverride() {
foreach ($this->mailIds as $id) {
// Turn on the email.
$this->config('user.settings')
->set('notify.' . $id, TRUE)
->save();
$default_body = $this->randomMachineName();
// Override the user.module template.
$this->config('user.mail')
->set($id . '.body', $default_body)
->save();
// Turn on Courier override.
$this->config('courier_system.settings')
->set('override.user_' . $id, TRUE)
->save();
$body = $this->randomMachineName();
$this->createGlobalTemplateCollection('courier_system.user_' . $id, $body);
// Simulate.
_user_mail_notify($id, $this->createUser());
$this->cron->run();
$this->assertMailString('body', $body, 1, 'Body found in override for ' . $id);
}
}
/**
* Create a random user for testing.
*
* @return \Drupal\user\UserInterface
* A user entity.
*/
protected function createUser() {
$mail = $this->randomMachineName() . '@' . $this->randomMachineName();
$account = User::create([
'name' => $this->randomMachineName(),
'mail' => $mail,
]);
$account->save();
return $account;
}
/**
* Create a global template collection and change its email body.
*
* @param $id
* The ID for the new global template collection.
* @param $email_body
* Change the email template body.
*
* @return \Drupal\courier\Entity\GlobalTemplateCollectionInterface
*/
protected function createGlobalTemplateCollection($id, $email_body) {
$gtc = GlobalTemplateCollection::create(['id' => $id]);
$gtc->save();
$gtc->getTemplateCollection()
->getTemplate('courier_email')
->setSubject($this->randomMachineName())
->setBody($email_body)->save();
return $gtc;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<php>
<!-- Set error reporting to E_ALL. -->
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
</php>
<testsuites>
<testsuite name="Courier">
<directory>./tests</directory>
<directory>./courier_system/tests</directory>
</testsuite>
</testsuites>
<!-- Filter for coverage reports. -->
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src</directory>
<exclude>
<directory>./src/Tests</directory>
<directory>./modules/courier_system/src/Tests</directory>
<directory>./courier_system/tests/src/Kernel</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
<?php
/**
* @file
* Searches for the core bootstrap file.
*/
$dir = __DIR__;
// Match against previous dir for Windows.
$previous_dir = '';
while ($dir = dirname($dir)) {
// We've reached the root.
if ($dir === $previous_dir) {
break;
}
$previous_dir = $dir;
if (is_file($dir . '/core/tests/bootstrap.php')) {
require_once $dir . '/core/tests/bootstrap.php';
return;
}
}
throw new RuntimeException('Unable to load core bootstrap.php.');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment