You are here

BookAccessDefaultsTest.php in Book access 1.x

Namespace

Drupal\book_access

File

tests/src/Kernel/BookAccessDefaultsTest.php
View source
<?php

namespace Drupal\book_access;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\RandomGeneratorTrait;
use Drupal\user\Entity\Role;

/**
 * Tests the BookAccessDefaults entity.
 *
 * @group book_access
 */
class BookAccessDefaultsTest extends KernelTestBase {
  use RandomGeneratorTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'system',
    'node',
    'user',
    'book',
    'book_access',
  ];

  /**
   * The storage manager.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $defaultStorage;

  /**
   * Create a role.
   *
   * @return \Drupal\Core\Entity\EntityBase|\Drupal\Core\Entity\EntityInterface
   *   The role
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function createRole() {
    $role1 = Role::create([
      'id' => 'test_role1',
      'name' => $this
        ->randomString(),
    ]);
    $role1
      ->save();
    return $role1;
  }

  /**
   * Setup common code for test cases.
   */
  protected function setup() : void {
    parent::setUp();
    $this
      ->installSchema('system', 'sequences');
    $this
      ->installConfig('system');
    $this
      ->installEntitySchema('user');
    $this
      ->installEntitySchema('node');
    $this
      ->installConfig('user');
    $this
      ->installConfig('book');
    $this
      ->installConfig('book_access');
    $this->defaultStorage = \Drupal::entityTypeManager()
      ->getStorage('book_access_defaults');
  }

  /**
   * Test that default config is created on install.   *.
   */
  public function testInstallCreatesDefaultConfig() {
    $query = $this->defaultStorage
      ->getQuery();
    $results = $query
      ->condition('grant_type', 'author')
      ->execute();
    $this
      ->assertIsArray($results, "Results are not available. Not an array");
    $this
      ->assertEqual(count($results), 1, "One result expected.");
    $query = \Drupal::entityTypeManager()
      ->getStorage('book_access_defaults')
      ->getQuery();
    $results = $query
      ->condition('grant_type', 'role')
      ->execute();
    $this
      ->assertIsArray($results, "Results are not available. Not an array");
    $this
      ->assertEqual(count($results), 3, "Three results expected.");
  }

  /**
   * Test that config can be updated.
   */
  public function testUpdatingConfig() {

    // Load initial config.
    $query = $this->defaultStorage
      ->getQuery();

    // Load authenticated role, expect FALSE for grant_add_child.
    $results = $query
      ->condition('role_id', 'authenticated')
      ->execute();
    $this
      ->assertIsArray($results, "Results are not available. Not an array");
    $this
      ->assertEqual(count($results), 1, "One result expected.");

    /** @var $config \Drupal\book_access\Entity\BookAccessDefaultsInterface */
    $config = $this->defaultStorage
      ->load(current($results));
    $grants = $config
      ->getGrants();
    $this
      ->assertEqual($grants['grant_add_child'], FALSE, "Grant add child expected FALSE.");

    // Update config.
    $grants['grant_add_child'] = TRUE;
    $config
      ->set('grants', $grants)
      ->save();

    // Reload, expect TRUE this time for grant_add_child.
    $query2 = $this->defaultStorage
      ->getQuery();
    $results = $query2
      ->condition('role_id', 'authenticated')
      ->execute();
    $this
      ->assertIsArray($results, "Results are not available. Not an array");
    $this
      ->assertEqual(count($results), 1, "One result expected.");

    /** @var $config \Drupal\book_access\Entity\BookAccessDefaultsInterface */
    $config = $this->defaultStorage
      ->load(current($results));
    $grants = $config
      ->getGrants();
    $this
      ->assertEqual($grants['grant_add_child'], TRUE, "Grant add child expected TRUE.");
  }

  /**
   * Test that adding a new role adds the corresponding BookAccessDefaults.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function testAddingRoleAddsConfig() {
    $role1 = $this
      ->createRole();
    $query = $this->defaultStorage
      ->getQuery();

    // Load BookAccessDefaults for new role.
    $results = $query
      ->condition('role_id', $role1
      ->id())
      ->execute();
    $this
      ->assertIsArray($results, "Results are not available. Not an array");
    $this
      ->assertEqual(count($results), 1, "One result expected.");
  }

  /**
   * Test that deleting a role deletes the corresponding BookAccessDefaults.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function testDeletingRoleDeletesConfig() {
    $role1 = $this
      ->createRole();
    $query = $this->defaultStorage
      ->getQuery();

    // Load BookAccessDefaults for new role.
    $results = $query
      ->condition('role_id', $role1
      ->id())
      ->execute();
    $this
      ->assertIsArray($results, "Results are not available. Not an array");
    $this
      ->assertEqual(count($results), 1, "One result expected.");
    $config = $this->defaultStorage
      ->load(current($results));
    $config_id = $config
      ->id();
    $role1
      ->delete();

    // Reload config.
    $config = NULL;
    $query = $this->defaultStorage
      ->getQuery();
    $results = $query
      ->condition('id', $config_id)
      ->execute();
    $this
      ->assertEqual(count($results), 0, "Zero results expected.");
  }

}

Classes

Namesort descending Description
BookAccessDefaultsTest Tests the BookAccessDefaults entity.