outline-8.x-1.x-dev/tests/src/Kernel/OutlineQueryAlterTest.php

tests/src/Kernel/OutlineQueryAlterTest.php
<?php

namespace Drupal\Tests\outline\Kernel;

use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\outline\Traits\OutlineTestTrait;

/**
 * Tests that appropriate query tags are added.
 *
 * @group outline
 */
class OutlineQueryAlterTest extends KernelTestBase {

  use OutlineTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'filter',
    'outline',
    'outline_test',
    'text',
    'user',
  ];

  /**
   * Tests that appropriate tags are added when querying the database.
   */
  public function testOutlineQueryAlter() {
    $this->installEntitySchema('outline_entry');

    // Create a new outline and add a few entries to it.
    $outline = $this->createOutline();
    $entries = [];
    for ($i = 0; $i < 5; $i++) {
      $entries[$i] = $this->createEntry($outline);
    }

    // Set up hierarchy. Entry 2 is a child of 1.
    $entries[2]->parent = $entries[1]->id();
    $entries[2]->save();

    /** @var \Drupal\outline\EntryStorageInterface $entry_storage */
    $entry_storage = $this->container->get('entity_type.manager')->getStorage('outline_entry');

    $this->setupQueryTagTestHooks();
    $loaded_entry = $entry_storage->load($entries[0]->id());
    // First entry was loaded.
    $this->assertEquals($entries[0]->id(), $loaded_entry->id());
    // EntryStorage::load().
    $this->assertQueryTagTestResult(1, 0);

    $this->setupQueryTagTestHooks();
    $loaded_entries = $entry_storage->loadTree($outline->id());
    // All entries were loaded.
    $this->assertCount(5, $loaded_entries);
    // EntryStorage::loadTree().
    $this->assertQueryTagTestResult(1, 1);

    $this->setupQueryTagTestHooks();
    $loaded_entries = $entry_storage->loadParents($entries[2]->id());
    // All parent entries were loaded.
    $this->assertCount(1, $loaded_entries);
    // EntryStorage::loadParents().
    $this->assertQueryTagTestResult(3, 1);

    $this->setupQueryTagTestHooks();
    $loaded_entries = $entry_storage->loadChildren($entries[1]->id());
    // All child entries were loaded.
    $this->assertCount(1, $loaded_entries);
    // EntryStorage::loadChildren().
    $this->assertQueryTagTestResult(3, 1);

    $this->setupQueryTagTestHooks();
    $connection = Database::getConnection();
    $query = $connection->select('outline_entry_data', 't');
    $query->addField('t', 'eid');
    $query->addTag('outline_entry_access');
    $eids = $query->execute()->fetchCol();
    // All entry IDs were retrieved.
    $this->assertCount(5, $eids);
    // Database custom ::select() with 'outline_entry_access' tag (preferred).
    $this->assertQueryTagTestResult(1, 1);

    $this->setupQueryTagTestHooks();
    $query = $connection->select('outline_entry_data', 't');
    $query->addField('t', 'eid');
    $query->addTag('entry_access');
    $eids = $query->execute()->fetchCol();
    // All entry IDs were retrieved.
    $this->assertCount(5, $eids);
    // Database custom ::select() with entry_access tag (deprecated).
    $this->assertQueryTagTestResult(1, 1);

    $this->setupQueryTagTestHooks();
    $query = \Drupal::entityQuery('outline_entry');
    $query->addTag('outline_entry_access');
    $result = $query->execute();
    // All entry IDs were retrieved.
    $this->assertCount(5, $result);
    // Custom entity query with outline_entry_access tag (preferred).
    $this->assertQueryTagTestResult(1, 1);

    $this->setupQueryTagTestHooks();
    $query = \Drupal::entityQuery('outline_entry');
    $query->addTag('entry_access');
    $result = $query->execute();
    // All entry IDs were retrieved.
    $this->assertCount(5, $result);
    // Custom entity query with outline_entry_access tag (preferred).
    $this->assertQueryTagTestResult(1, 1);
  }

  /**
   * Sets up the hooks in the test module.
   */
  protected function setupQueryTagTestHooks() {
    outline_entrys_static_reset();
    $state = $this->container->get('state');
    $state->set('outline_test_query_alter', 0);
    $state->set('outline_test_query_entry_access_alter', 0);
    $state->set('outline_test_query_outline_entry_access_alter', 0);
  }

  /**
   * Verifies invocation of the hooks in the test module.
   *
   * @param int $expected_generic_invocations
   *   The number of times the generic query_alter hook is expected to have
   *   been invoked.
   * @param int $expected_specific_invocations
   *   The number of times the tag-specific query_alter hooks are expected to
   *   have been invoked.
   */
  protected function assertQueryTagTestResult($expected_generic_invocations, $expected_specific_invocations) {
    $state = $this->container->get('state');
    $this->assertEquals($expected_generic_invocations, $state->get('outline_test_query_alter'));
    $this->assertEquals($expected_specific_invocations, $state->get('outline_test_query_entry_access_alter'));
    $this->assertEquals($expected_specific_invocations, $state->get('outline_test_query_outline_entry_access_alter'));
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc