ForwardRevisionTest.php 3.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
<?php

namespace Drupal\Tests\taxonomy\Kernel;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;

/**
 * Kernel tests for taxonomy forward revisions.
 *
 * @group taxonomy
 */
class ForwardRevisionTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = ['taxonomy', 'node', 'user', 'text', 'field', 'system'];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installEntitySchema('taxonomy_term');
    $this->installSchema('node', 'node_access');
  }

  /**
   * Tests that the taxonomy index work correctly with forward revisions.
   */
  public function testTaxonomyIndexWithForwardRevision() {
    \Drupal::configFactory()->getEditable('taxonomy.settings')->set('maintain_index_table', TRUE)->save();

    Vocabulary::create([
      'name' => 'test',
      'vid' => 'test',
    ])->save();
    $term = Term::create([
      'name' => 'term1',
      'vid' => 'test',
    ]);
    $term->save();
    $term2 = Term::create([
      'name' => 'term2',
      'vid' => 'test',
    ]);
    $term2->save();

    NodeType::create([
      'type' => 'page',
    ])->save();

    FieldStorageConfig::create([
      'entity_type' => 'node',
      'field_name' => 'field_tags',
      'type' => 'entity_reference',
      'settings' => [
        'target_type' => 'taxonomy_term',
      ],
    ])->save();

    FieldConfig::create([
      'field_name' => 'field_tags',
      'entity_type' => 'node',
      'bundle' => 'page',
    ])->save();
    $node = Node::create([
      'type' => 'page',
      'title' => 'test_title',
      'field_tags' => [$term->id()],
    ]);
    $node->save();

    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);

    // Normal new revision.
    $node->setNewRevision(TRUE);
    $node->isDefaultRevision(TRUE);
    $node->field_tags->target_id = $term2->id();
    $node->save();

    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);

    // Check that saving a forward (non-default) revision does not affect the
    // taxonomy index.
    $node->setNewRevision(TRUE);
    $node->isDefaultRevision(FALSE);
    $node->field_tags->target_id = $term->id();
    $node->save();

    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);

    // Check that making the previously created forward-revision the default
    // revision updates the taxonomy index correctly.
    $node->isDefaultRevision(TRUE);
    $node->save();

    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
  }

  protected function getTaxonomyIndex() {
    return \Drupal::database()->select('taxonomy_index')
116 117 118
      ->fields('taxonomy_index')
      ->execute()
      ->fetchAllAssoc('nid');
119 120 121
  }

}