View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\site_alert\Kernel;
use Drupal\Tests\site_alert\Traits\SiteAlertTestTrait;
class CliCommandsTest extends SiteAlertKernelTestBase {
use SiteAlertTestTrait;
protected $cliCommands;
protected $siteAlertStorage;
protected function setUp() {
parent::setUp();
$this->siteAlertStorage = $this->entityTypeManager
->getStorage('site_alert');
$this->cliCommands = $this->container
->get('site_alert.cli_commands');
}
public function testCreateSiteAlert() : void {
$label = 'phpunit_test_create';
$message = "A nice bit of message here.";
$severity = 'low';
$options = [
'severity' => $severity,
];
$this->cliCommands
->create($label, $message, $options);
$site_alert = $this
->loadAlertByLabel($label);
$this
->assertAlert($site_alert, $label, $message, $severity, TRUE);
}
public function testCreateSiteAlertWithOptions(?string $severity, ?string $start = NULL, ?string $end = NULL) : void {
$label = 'phpunit_test_create';
$message = "A nice bit of message here.";
$severity = 'low';
$start = '2022-09-12T15:30:01';
$end = '2022-09-13T15:45:01';
$options = [
'severity' => $severity,
'start' => $start,
'end' => $end,
];
$this->cliCommands
->create($label, $message, $options);
$expected_severity = $severity ?? 'medium';
$site_alert = $this
->loadAlertByLabel($label);
$this
->assertAlert($site_alert, $label, $message, $expected_severity, TRUE, $options);
}
public function createSiteAlertWithOptionsProvider() : array {
return [
[
'low',
'2022-09-12T15:30:01',
'2022-09-13T15:45:01',
],
[
NULL,
'2022-09-12T15:30:01',
'2022-09-13T15:45:01',
],
[
'medium',
NULL,
'2022-09-13T15:45:01',
],
[
'high',
'2022-09-12T15:30:01',
NULL,
],
[
NULL,
NULL,
'2022-09-13T15:45:01',
],
[
NULL,
'2022-09-12T15:30:01',
NULL,
],
[
'low',
NULL,
NULL,
],
[
NULL,
NULL,
NULL,
],
];
}
public function testDeleteSiteAlert() : void {
$label = 'phpunit_test_delete';
$message = "A nice bit of message here.";
$options = [
'severity' => 'low',
];
$this->cliCommands
->create($label, $message, $options);
$result = $this->cliCommands
->delete($label);
$this
->assertGreaterThan(0, $result);
$siteAlerts = $this
->loadAlertsByLabel($label);
$this
->assertCount(0, $siteAlerts);
}
public function testDisableSiteAlertValidation() : void {
$this
->expectException(\InvalidArgumentException::class);
$this->cliCommands
->disable('some-non-existing-site-alert');
}
public function testDisableSiteAlertByLabel() : void {
$label = 'phpunit_test_disable';
$message = 'This alert is active, for now.';
$this->cliCommands
->create($label, $message, []);
$this
->assertActiveAlertCount(1);
$count = $this->cliCommands
->disable('phpunit_test_disable');
$this
->assertEquals(1, $count);
$this
->assertActiveAlertCount(0);
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage("No active site alerts found with the label 'phpunit_test_disable'.");
$this->cliCommands
->disable('phpunit_test_disable');
}
public function testDisableAllSiteAlerts() : void {
$alerts = [
[
'phpunit_test_disable_1',
'An active alert',
[],
],
[
'phpunit_test_disable_2',
'Another active alert',
[],
],
[
'phpunit_test_disable_3',
'An inactive alert',
[
'active' => FALSE,
],
],
];
foreach ($alerts as [
$label,
$message,
$options,
]) {
$this->cliCommands
->create($label, $message, $options);
}
$this
->assertActiveAlertCount(2);
$count = $this->cliCommands
->disable(NULL, [
'all' => TRUE,
]);
$this
->assertEquals(2, $count);
$this
->assertActiveAlertCount(0);
$count = $this->cliCommands
->disable(NULL, [
'all' => TRUE,
]);
$this
->assertEquals(0, $count);
$this
->assertActiveAlertCount(0);
}
public function testEnableSiteAlertValidation() : void {
$this
->expectException(\InvalidArgumentException::class);
$this->cliCommands
->enable('some-non-existing-site-alert');
}
public function testEnableSiteAlertByLabel() : void {
$label = 'phpunit_test_enable';
$message = 'This alert is not yet active.';
$this->cliCommands
->create($label, $message, [
'active' => FALSE,
]);
$this
->assertInactiveAlertCount(1);
$count = $this->cliCommands
->enable('phpunit_test_enable');
$this
->assertEquals(1, $count);
$this
->assertActiveAlertCount(1);
$this
->assertInactiveAlertCount(0);
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage("No inactive site alerts found with the label 'phpunit_test_enable'.");
$this->cliCommands
->enable('phpunit_test_enable');
}
}