AdminSettingsTest.php in Sparkpost email 8.2
File
tests/src/Functional/AdminSettingsTest.php
View source
<?php
namespace Drupal\Tests\sparkpost\Functional;
use Drupal\Tests\BrowserTestBase;
class AdminSettingsTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'sparkpost',
];
protected $user;
protected $admin;
public function setUp() : void {
parent::setUp();
$this->user = $this
->drupalCreateUser();
$this->admin = $this
->drupalCreateUser([
'administer sparkpost',
]);
}
public function testAdminAccess() {
$this
->drupalLogin($this->user);
$this
->drupalGet('admin/config/services/sparkpost');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('admin/config/services/sparkpost/test');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogout();
$this
->drupalLogin($this->admin);
$this
->drupalGet('admin/config/services/sparkpost');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/config/services/sparkpost/test');
$this
->assertSession()
->statusCodeEquals(403);
\Drupal::configFactory()
->getEditable('sparkpost.settings')
->set('api_key', 'API_KEY')
->save();
$this
->drupalGet('admin/config/services/sparkpost/test');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testSkipQueueOnCron() {
\Drupal::configFactory()
->getEditable('sparkpost.settings')
->set('skip_cron', FALSE)
->set('async', TRUE)
->save();
$queue = \Drupal::queue('sparkpost_send');
$queue
->createItem($this
->createDummyMessage());
self::assertEquals($queue
->numberOfItems(), 1);
$cron = \Drupal::service('cron');
$cron
->run();
self::assertEquals($queue
->numberOfItems(), 0);
\Drupal::configFactory()
->getEditable('sparkpost.settings')
->set('skip_cron', TRUE)
->save();
\Drupal::service('plugin.cache_clearer')
->clearCachedDefinitions();
$queue
->createItem($this
->createDummyMessage());
self::assertEquals($queue
->numberOfItems(), 1);
$cron
->run();
self::assertEquals($queue
->numberOfItems(), 1);
}
private function createDummyMessage() {
$message = \Drupal::service('sparkpost.message_wrapper');
$message
->setDrupalMessage([]);
$message
->setSparkpostMessage([]);
return $message;
}
}