CourierGlobalCollectionTest.php in Courier 8
File
tests/src/Kernel/CourierGlobalCollectionTest.php
View source
<?php
namespace Drupal\Tests\courier\Kernel;
use Drupal\courier\Entity\GlobalTemplateCollection;
use Drupal\courier\TemplateCollectionInterface;
use Drupal\courier_test_message\Entity\TestMessage;
class CourierGlobalCollectionTest extends CourierKernelTestBase {
public static $modules = [
'courier_test_message',
'user',
];
protected $gtcService;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('courier_template_collection');
$this
->installEntitySchema('courier_test_message');
$this
->installEntitySchema('courier_email');
$this
->installEntitySchema('user');
$this->gtcService = $this->container
->get('courier.manager.global_template_collection');
}
public function testCreateTemplateCollection() {
$content = $this
->randomString();
$gtc = GlobalTemplateCollection::create([
'id' => $this
->randomMachineName(),
])
->setTemplate('courier_test_message', [
'message' => $content,
]);
$gtc
->save();
$tc = $this->gtcService
->getLocalCollection($gtc);
$this
->assertTrue($tc instanceof TemplateCollectionInterface);
$this
->assertEquals(2, count($tc
->getTemplates()), 'Templates for courier_email and courier_test_message.');
$this
->assertEquals($content, $tc
->getTemplate('courier_test_message')
->getMessage());
}
public function testGlobalTemplateCollectionExceptionUnsaved() {
$gtc = GlobalTemplateCollection::create([
'id' => $this
->randomMachineName(),
])
->setTemplate('courier_test_message', [
'message' => $this
->randomString(),
]);
$this
->setExpectedException('Drupal\\courier\\Exception\\GlobalTemplateCollectionException');
$this->gtcService
->getLocalCollection($gtc);
}
public function testGlobalTemplateCollectionLiveImportTemplate() {
$old_message = $this
->randomString();
$gtc = GlobalTemplateCollection::create([
'id' => $this
->randomMachineName(),
])
->setTemplate('courier_test_message', [
'message' => $old_message,
]);
$gtc
->save();
$tc = $this->gtcService
->getLocalCollection($gtc);
$template_id = $tc
->getTemplate('courier_test_message')
->id();
$new_message = $this
->randomString();
$gtc
->setTemplate('courier_test_message', [
'message' => $new_message,
])
->save();
\Drupal::entityTypeManager()
->getStorage('courier_test_message')
->resetCache();
$template = TestMessage::load($template_id);
$this
->assertEquals($new_message, $template
->getMessage());
}
public function testTemplateUpdatesGlobalTemplateCollection() {
$old_message = $this
->randomString();
$gtc = GlobalTemplateCollection::create([
'id' => $this
->randomMachineName(),
])
->setTemplate('courier_test_message', [
'message' => $old_message,
]);
$gtc
->save();
$tc = $this->gtcService
->getLocalCollection($gtc);
$template_id = $tc
->getTemplate('courier_test_message')
->id();
$new_message = $this
->randomString();
TestMessage::load($template_id)
->setMessage($new_message)
->save();
$gtc = GlobalTemplateCollection::load($gtc
->id());
$this
->assertEquals($new_message, $gtc
->getTemplate('courier_test_message')['message']);
}
}