You are here

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;

/**
 * Test the entity validation.
 *
 * @group readonlymode
 */
class ReadonlymodeKernelTest extends EntityKernelTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'system',
    'user',
    'readonlymode',
  ];

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * Make sure that entities fail validation during read-only maintenance mode.
   */
  public function testReadonlyValidation() {
    $existing = $this
      ->createUser();
    $violations = $existing
      ->validate();
    self::assertEquals(0, $violations
      ->count());

    /** @var \Drupal\readonlymode\ReadonlymodeManager $manager */
    $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,
    ]);

    // This warning is configurable, here we test also the token replacement.
    $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());
  }

}

Classes

Namesort descending Description
ReadonlymodeKernelTest Test the entity validation.