You are here

public function DynamicDepthCalculationTest::testCalculateDepth in Taxonomy Term Depth 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/DynamicDepthCalculationTest.php \Drupal\Tests\taxonomy_term_depth\Functional\DynamicDepthCalculationTest::testCalculateDepth()

Creates and ensures that a feed is unique, checks source, and deletes feed.

1 call to DynamicDepthCalculationTest::testCalculateDepth()
DynamicDepthCalculationTest::testCronQueue in tests/src/Functional/DynamicDepthCalculationTest.php
Test cron queue.

File

tests/src/Functional/DynamicDepthCalculationTest.php, line 38

Class

DynamicDepthCalculationTest
Getting calculating dynamically the depth of the term.

Namespace

Drupal\Tests\taxonomy_term_depth\Functional

Code

public function testCalculateDepth() {
  $voc = Vocabulary::create([
    'vid' => 'testvoc',
    'name' => 'testvoc',
    'description' => 'testvoc',
  ]);
  $voc
    ->save();

  /** @var \Drupal\taxonomy\TermInterface $term1 */
  $term1 = Term::create([
    'vid' => $voc
      ->id(),
    'name' => 'Depth 1 term',
  ]);
  $term1
    ->save();

  /** @var \Drupal\taxonomy\TermInterface $term2 */
  $term2 = Term::create([
    'vid' => $voc
      ->id(),
    'name' => 'Depth 2 term',
  ]);
  $term2->parent
    ->set(0, $term1
    ->id());
  $term2
    ->save();

  /** @var \Drupal\taxonomy\TermInterface $term3 */
  $term3 = Term::create([
    'vid' => $voc
      ->id(),
    'name' => 'Depth 2 term',
  ]);
  $term3->parent
    ->set(0, $term3
    ->id());
  $term3->parent
    ->set(1, $term2
    ->id());
  $term3
    ->save();
  $this
    ->assertEqual(taxonomy_term_depth_get_by_tid($term1
    ->id()), 1, 'Depth of first term');
  $this
    ->assertEqual(taxonomy_term_depth_get_by_tid($term2
    ->id()), 2, 'Depth of second term');
  $this
    ->assertEqual(taxonomy_term_depth_get_by_tid($term3
    ->id()), 3, 'Depth of third term');
  $this
    ->assertEqual($term1->depth_level
    ->first() ? $term1->depth_level
    ->first()->value : NULL, 1, 'Saved depth of first term');
  $this
    ->assertEqual($term2->depth_level
    ->first() ? $term2->depth_level
    ->first()->value : NULL, 2, 'Saved depth of second term');
  $this
    ->assertEqual($term3->depth_level
    ->first() ? $term3->depth_level
    ->first()->value : NULL, 3, 'Saved depth of third term');
  $chain = taxonomy_term_depth_get_full_chain($term2
    ->id());
  $compare = [
    $term1
      ->id(),
    $term2
      ->id(),
    $term3
      ->id(),
  ];
  $this
    ->assertTrue($chain === $compare, 'Testing fullchain for term2');
  $chain = taxonomy_term_depth_get_full_chain($term2
    ->id(), TRUE);
  $this
    ->assertTrue($chain === array_reverse($compare), 'Testing reversed fullchain for term2');
  $this
    ->assertEqual(\Drupal::database()
    ->query('SELECT depth_level FROM {taxonomy_term_field_data} WHERE tid=:tid', [
    ':tid' => $term1
      ->id(),
  ])
    ->fetchField(), 1, 'DB depth_level field of first term');
  $this
    ->assertEqual(\Drupal::database()
    ->query('SELECT depth_level FROM {taxonomy_term_field_data} WHERE tid=:tid', [
    ':tid' => $term2
      ->id(),
  ])
    ->fetchField(), 2, 'DB depth_level field of second term');
  $this
    ->assertEqual(\Drupal::database()
    ->query('SELECT depth_level FROM {taxonomy_term_field_data} WHERE tid=:tid', [
    ':tid' => $term3
      ->id(),
  ])
    ->fetchField(), 3, 'DB depth_level field of third term');
}