outline-8.x-1.x-dev/tests/src/Functional/EntryIndexTest.php

tests/src/Functional/EntryIndexTest.php
<?php

namespace Drupal\Tests\outline\Functional;

use Drupal\Core\Link;
use Drupal\Core\Database\Database;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

/**
 * Tests the hook implementations that maintain the outline index.
 *
 * @group outline
 */
class EntryIndexTest extends OutlineTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = ['views'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Outline for testing.
   *
   * @var \Drupal\outline\OutlineInterface
   */
  protected $outline;

  /**
   * Name of the outline entry reference field.
   *
   * @var string
   */
  protected $fieldName1;

  /**
   * Name of the outline entry reference field.
   *
   * @var string
   */
  protected $fieldName2;

  protected function setUp(): void {
    parent::setUp();

    // Create an administrative user.
    $this->drupalLogin($this->drupalCreateUser([
      'administer outline',
      'bypass node access',
    ]));

    // Create a outline and add two entry reference fields to article nodes.
    $this->outline = $this->createOutline();

    $this->fieldName1 = mb_strtolower($this->randomMachineName());
    $handler_settings = [
      'target_bundles' => [
        $this->outline->id() => $this->outline->id(),
       ],
      'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'outline_entry', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('node', 'article')
      ->setComponent($this->fieldName1, [
        'type' => 'options_select',
      ])
      ->save();
    $display_repository->getViewDisplay('node', 'article')
      ->setComponent($this->fieldName1, [
        'type' => 'entity_reference_label',
      ])
      ->save();

    $this->fieldName2 = mb_strtolower($this->randomMachineName());
    $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'outline_entry', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('node', 'article')
      ->setComponent($this->fieldName2, [
        'type' => 'options_select',
      ])
      ->save();
    $display_repository->getViewDisplay('node', 'article')
      ->setComponent($this->fieldName2, [
        'type' => 'entity_reference_label',
      ])
      ->save();
  }

  /**
   * Tests that the outline index is maintained properly.
   */
  public function testOutlineIndex() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    // Create entries in the outline.
    $entry_1 = $this->createEntry($this->outline);
    $entry_2 = $this->createEntry($this->outline);

    // Post an article.
    $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $edit['body[0][value]'] = $this->randomMachineName();
    $edit["{$this->fieldName1}[]"] = $entry_1->id();
    $edit["{$this->fieldName2}[]"] = $entry_1->id();
    $this->drupalPostForm('node/add/article', $edit, t('Save'));

    // Check that the entry is indexed, and only once.
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $connection = Database::getConnection();
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_1->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 1 is indexed once.');

    // Update the article to change one entry.
    $edit["{$this->fieldName1}[]"] = $entry_2->id();
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));

    // Check that both entries are indexed.
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_1->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 1 is indexed.');
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_2->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 2 is indexed.');

    // Update the article to change another entry.
    $edit["{$this->fieldName2}[]"] = $entry_2->id();
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));

    // Check that only one entry is indexed.
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_1->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(0, $index_count, 'Entry 1 is not indexed.');
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_2->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 2 is indexed once.');

    // Redo the above tests without interface.
    $node_storage->resetCache([$node->id()]);
    $node = $node_storage->load($node->id());
    $node->title = $this->randomMachineName();

    // Update the article with no entry changed.
    $node->save();

    // Check that the index was not changed.
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_1->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(0, $index_count, 'Entry 1 is not indexed.');
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_2->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 2 is indexed once.');

    // Update the article to change one entry.
    $node->{$this->fieldName1} = [['target_id' => $entry_1->id()]];
    $node->save();

    // Check that both entries are indexed.
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_1->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 1 is indexed.');
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_2->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 2 is indexed.');

    // Update the article to change another entry.
    $node->{$this->fieldName2} = [['target_id' => $entry_1->id()]];
    $node->save();

    // Check that only one entry is indexed.
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_1->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(1, $index_count, 'Entry 1 is indexed once.');
    $index_count = $connection->select('outline_index')
      ->condition('nid', $node->id())
      ->condition('eid', $entry_2->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(0, $index_count, 'Entry 2 is not indexed.');
  }

  /**
   * Tests that there is a link to the parent entry on the child entry page.
   */
  public function testOutlineEntryHierarchyBreadcrumbs() {
    // Create two outline entries and set entry2 as the parent of entry1.
    $entry1 = $this->createEntry($this->outline);
    $entry2 = $this->createEntry($this->outline);
    $entry1->parent = [$entry2->id()];
    $entry1->save();

    // Verify that the page breadcrumbs include a link to the parent entry.
    $this->drupalGet('outline/entry/' . $entry1->id());
    // Breadcrumbs are not rendered with a language, prevent the entry
    // language from being added to the options.
    $this->assertRaw(Link::fromTextAndUrl($entry2->getName(), $entry2->toUrl('canonical', ['language' => NULL]))->toString(), 'Parent entry link is displayed when viewing the node.');
  }

}

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

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