View source
<?php
namespace Drupal\Tests\automatic_updates\Functional;
use Drupal\automatic_updates\Event\PostUpdateEvent;
use Drupal\automatic_updates\UpdateMetadata;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class NotifyTest extends BrowserTestBase {
use AssertMailTrait;
use StringTranslationTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'automatic_updates',
'test_automatic_updates',
'update',
];
protected $user;
protected function setUp() {
parent::setUp();
$end_point = $this
->buildUrl(Url::fromRoute('test_automatic_updates.json_test_controller'));
$this
->config('automatic_updates.settings')
->set('psa_endpoint', $end_point)
->save();
$this
->config('update.settings')
->set('notification.emails', [
'admin@example.com',
])
->save();
$this->user = $this
->drupalCreateUser([
'administer site configuration',
'access administration pages',
]);
$this
->drupalLogin($this->user);
}
public function testPsaMail() {
$this
->drupalGet(Url::fromRoute('system.admin'));
$this
->assertSession()
->pageTextContains('Critical Release - SA-2019-02-19');
$notify = $this->container
->get('automatic_updates.psa_notify');
$notify
->send();
$this
->assertCount(1, $this
->getMails());
$this
->assertMailString('subject', '3 urgent Drupal announcements require your attention', 1);
$this
->assertMailString('body', 'Critical Release - SA-2019-02-19', 1);
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this->container
->get('state')
->delete('automatic_updates.last_check');
$this
->config('automatic_updates.settings')
->set('enable_psa', FALSE)
->save();
$notify
->send();
$this
->assertCount(0, $this
->getMails());
}
public function testPostUpdateMail() {
$metadata = new UpdateMetadata('drupal', 'core', '8.7.0', '8.8.0');
$post_update = new PostUpdateEvent($metadata, TRUE);
$notify = $this->container
->get('automatic_updates.post_update_subscriber');
$notify
->onPostUpdate($post_update);
$this
->assertCount(1, $this
->getMails());
$this
->assertMailString('subject', 'Automatic update of "drupal" succeeded', 1);
$this
->assertMailString('body', 'The project "drupal" was updated from "8.7.0" to "8.8.0" with success.', 1);
$this->container
->get('state')
->set('system.test_mail_collector', []);
$post_update = new PostUpdateEvent($metadata, FALSE);
$notify = $this->container
->get('automatic_updates.post_update_subscriber');
$notify
->onPostUpdate($post_update);
$this
->assertCount(1, $this
->getMails());
$this
->assertMailString('subject', 'Automatic update of "drupal" failed', 1);
$this
->assertMailString('body', 'The project "drupal" was updated from "8.7.0" to "8.8.0" with failures.', 1);
}
}