ReadonlymodeKernelTest.php in Read only mode 2.0.x
File
tests/src/Kernel/ReadonlymodeKernelTest.php
View source
<?php
namespace Drupal\Tests\readonlymode\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\user\Entity\User;
class ReadonlymodeKernelTest extends EntityKernelTestBase {
public static $modules = [
'system',
'user',
'readonlymode',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'readonlymode',
]);
$this
->config('system.site')
->set('name', 'Drupal')
->save();
$this
->config('readonlymode.settings')
->set('messages.not_saved', 'Data not saved. [site:name] is currently under maintenance.')
->save();
}
public function testReadonlyValidation() {
$existing = $this
->createUser();
$violations = $existing
->validate();
self::assertEquals(0, $violations
->count());
$manager = \Drupal::service('readonlymode.manager');
self::assertFalse($manager
->isReadonly());
$manager
->setReadonly(TRUE);
self::assertTrue($manager
->isReadonly());
$name = $this
->randomMachineName();
$new = User::create([
'name' => $name,
'mail' => $name . '@example.com',
'pass' => \Drupal::service('password_generator')
->generate(),
'status' => 1,
]);
$warning = 'Data not saved. Drupal is currently under maintenance.';
$violations = $new
->validate();
self::assertEquals(1, $violations
->count());
self::assertEquals($warning, $violations[0]
->getMessage());
$violations = $existing
->validate();
self::assertEquals(1, $violations
->count());
self::assertEquals($warning, $violations[0]
->getMessage());
}
}