Hopefully we can leverage existing tests in the core taxonomy module.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cameronprince created an issue. See original summary.

cameron prince’s picture

In reviewing core taxonomy module's tests, I believe we need these three for starters:

  1. TaxonomyIndexTidUiTest.php
  2. TaxonomyTermArgumentDepthTest.php
  3. TaxonomyTermFilterDepthTest.php

You can see them here: https://git.drupalcode.org/project/drupal/blob/8.8.x/core/modules/taxono...

I'm going to get started by extending TaxonomyIndexTidUiTest.php and seeing what all needs modifying.

cameron prince’s picture

Status: Active » Needs work
FileSize
18.84 KB

This patch stubs out testing, but still needs work.

The test view needs to be updated to use the TEI filter plugin instead of core's and something is causing:

$ ../vendor/bin/phpunit --configuration core modules/contrib/taxonomy_entity_index
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
Testing modules/contrib/taxonomy_entity_index
E                                                                   1 / 1 (100%)
Time: 1.18 minutes, Memory: 6.00MB
There was 1 error:
1) Drupal\Tests\taxonomy_entity_index\Functional\Views\TaxonomyEntityIndexTermFilterDepthTest::testTermWithDepthFilter
Drupal\Core\Entity\EntityStorageException: 'node_type' entity with ID 'article' already exists.
docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php:490
docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php:454
docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php:263
docroot/core/lib/Drupal/Core/Entity/EntityBase.php:395
docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php:613
docroot/core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php:41
docroot/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTestBase.php:84
docroot/modules/contrib/taxonomy_entity_index/tests/src/Functional/Views/TaxonomyEntityIndexTestBase.php:24
docroot/modules/contrib/taxonomy_entity_index/tests/src/Functional/Views/TaxonomyEntityIndexTermFilterDepthTest.php:44
ERRORS!
Tests: 1, Assertions: 3, Errors: 1.

I'm out of time for now and the 1.1 release needs to go out. Hopefully, we'll get tests in 1.2.

larowlan’s picture

Status: Needs work » Needs review
FileSize
2.8 KB
18.51 KB

This passes locally

larowlan’s picture

can you enable testing on https://www.drupal.org/node/1185660/qa so testbot can test patches?

larowlan’s picture

FileSize
8.91 KB
27.43 KB

Here's a kernel test that covers the basic entity CRUD operations and a base test that I'll make use of in #3108170: Add a formatter that does 'backfilling' of entity references (more like this, similar by term)

larowlan’s picture

+++ b/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
@@ -0,0 +1,193 @@
+  /**
+   * Tests operations that insert/update/delete to the index table.
+   */
+  public function testWritingToIndexTable() {
+    $vocab = $this->createVocabulary();
+    $term1 = $this->createTerm($vocab);
+    $term2 = $this->createTerm($vocab);
+    $term3 = $this->createTerm($vocab);
+    $term4 = $this->createTerm($vocab);
+    $this->assertThatDisabledEntityTypesDoNotWriteToTheIndex([$term1, $term2]);
+    $entity = $this->assertThatEntityInsertWritesToTheIndex([$term1, $term2, $term3]);
+    $entity = $this->assertThatEntityUpdatesModifyTheIndex($entity, [$term1, $term2, $term4]);
+    $this->assertThatTermDeletionUpdatesTheIndex($entity, [$term1, $term2], [$term4]);
+    $this->assertThatEntityDeletionUpdatesTheIndex($entity);

this is the kernel test in a nutshell 🥜

Lendude’s picture

  1. +++ b/tests/src/Functional/Views/TaxonomyEntityIndexTermFilterDepthTest.php
    @@ -0,0 +1,145 @@
    +    // Top-level term, depth 1.
    +    $expected = [['nid' => 4]];
    +    $this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected);
    

    comment and test don't align, the test should have depth 1 I think

  2. +++ b/tests/src/Functional/Views/TaxonomyEntityIndexTermFilterDepthTest.php
    @@ -0,0 +1,145 @@
    +   * Changes the tid filter to given term and depth.
    

    should probably describe what it asserts too

  3. +++ b/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
    @@ -0,0 +1,193 @@
    +    entity_test_create_bundle('termz');
    +    return ['termz'];
    +  }
    

    Why isn't $entity_type_id used? And shouldn't you be able to pass 'termz' and not have to hard code it here?

  4. +++ b/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
    @@ -0,0 +1,193 @@
    +  protected function assertThatTermDeletionUpdatesTheIndex(EntityTest $entity, array $expected_terms, array $terms_to_delete) {
    

    missing docblock although the method name is pretty descriptive :)

  5. +++ b/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
    @@ -0,0 +1,193 @@
    +  protected function assertThatEntityDeletionUpdatesTheIndex(EntityTest $entity) {
    +    $entity->delete();
    +    $this->assertCount(0, $this->database->select('taxonomy_entity_index', 'tei')
    

    Might be nice to test that it only deletes the records of the passed entity and not all records? Since the assertion now assumes that this is the same thing.

The module description states : "The core {taxonomy_index} table only maintains its data for published nodes. This module maintains its data for all entity types (regardless of published or not) and also indexed by revision."
I'm not seeing any coverage for the unpublished and revisions part.

larowlan’s picture

Title: Create tests » Create tests and a config schema for taxonomy_entity_index.settings
larowlan’s picture

gambry’s picture

Status: Needs review » Reviewed & tested by the community
Related issues: +#3109722: Entity IDs can be strings in Drupal 8
  1. +++ b/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
    @@ -0,0 +1,219 @@
    +   * Assert that deleting a term deletes the item from the index..
    

    Double ...

  2. +++ b/tests/src/Functional/Views/TaxonomyEntityIndexTermFilterDepthTest.php
    @@ -0,0 +1,145 @@
    +use Drupal\Tests\taxonomy\Functional\Views\TaxonomyTermFilterDepthTest;
    

    This is unused and can be removed?

  3. +++ b/tests/modules/taxonomy_entity_index_test_views/test_views/views.view.test_argument_taxonomy_entity_index_index_tid_depth.yml
    @@ -0,0 +1,226 @@
    +base_table: node_field_data
    

    Just a consideration... we are testing the module against views listing nodes... and not custom entities which the module is mostly - only, really - used for. Is there a reason?

  4. +++ b/tests/src/Kernel/TaxonomyEntityIndexKernelTestBase.php
    @@ -0,0 +1,105 @@
    +use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
    

    I don't think this is a big issue, but this Trait has been introduced moved in this namespace in 8.8.x and so instances using 8.7 can't use this, without class_alias(). An issue? I don't think so, but worth mentioning.

All good then. I'm RTBCing this as 1) and 2) can be fixed on commit and the other two are more considerations rather than requests.
Thanks. When this is in we can unblock #3109722: Entity IDs can be strings in Drupal 8.

And finally RE last comment from #8:

The module description states : "The core {taxonomy_index} table only maintains its data for published nodes. This module maintains its data for all entity types (regardless of published or not) and also indexed by revision."
I'm not seeing any coverage for the unpublished and revisions part.

I don't think this has been actioned, but probably can be done in a follow up?

The last submitted patch, 3: 3104318-3.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

gambry’s picture

Status: Reviewed & tested by the community » Needs work

I need to re-open this as the schema for the views plugins are missing. Something like (please double-check!):

views.filter.taxonomy_entity_index_tid_depth:
  type: views.filter.taxonomy_index_tid_depth
  label: 'Taxonomy Entity Index term ID with depth'

views.argument.taxonomy_entity_index_tid_depth:
  type: views.argument.taxonomy_index_tid_depth
  label: 'Taxonomy Entity Index term ID with depth'

views.field.taxonomy_entity_index_tid:
  type: views.field.taxonomy_index_tid
  label: 'Taxonomy Entity Index term ID'

Without this if using the module's filter plugin and select any term for Limit list to selected items, Views UI throws a InvalidArgumentException: The configuration property display.default.display_options.filters.taxonomy_entity_index_tid_depth.value.123 doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() because the value data types defaults to views.filter_value.* which is expecting a string. And I presume similar errors may be thrown in some scenarios for the others plugins too.

gambry’s picture

Status: Needs work » Reviewed & tested by the community

Forget about #14, I checked out the wrong commit. All looks good, although suggestions on #12 to be done on commit still apply.

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed this to 8.x-1.x

Fixed this on commit

index 0365ebb..e930f80 100644
--- a/tests/src/Functional/Views/TaxonomyEntityIndexTermFilterDepthTest.php
+++ b/tests/src/Functional/Views/TaxonomyEntityIndexTermFilterDepthTest.php
@@ -3,7 +3,6 @@
 namespace Drupal\Tests\taxonomy_entity_index\Functional\Views;

 use Drupal\node\NodeInterface;
-use Drupal\Tests\taxonomy\Functional\Views\TaxonomyTermFilterDepthTest;
 use Drupal\views\Views;

 /**
diff --git a/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php b/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
index 788ddab..3b5815f 100644
--- a/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
+++ b/tests/src/Kernel/TaxonomyEntityIndexEntityTest.php
@@ -159,7 +159,7 @@ class TaxonomyEntityIndexEntityTest extends TaxonomyEntityIndexKernelTestBase {
   }

   /**
-   * Assert that deleting a term deletes the item from the index..
+   * Assert that deleting a term deletes the item from the index.
    *
    * @param \Drupal\entity_test\Entity\EntityTest $entity
    *   Test entity.

I'll open a follow-up for the last item on #8 - thanks all.

larowlan’s picture

  • larowlan committed f761c9b on 8.x-1.x
    Issue #3104318 by larowlan, cameronprince, gambry, Lendude: Create tests...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.