You are here

public function CatalogBlockTest::testExpandCategories in Ubercart 8.4

Tests the expand catalog categories functionality.

File

uc_catalog/tests/src/Functional/CatalogBlockTest.php, line 106

Class

CatalogBlockTest
Tests the catalog block functionality.

Namespace

Drupal\Tests\uc_catalog\Functional

Code

public function testExpandCategories() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

  // Top level category with two children.
  $parent = $this
    ->createCatalogTerm();
  $child = [];
  $child[1] = $this
    ->createCatalogTerm([
    'parent' => $parent
      ->id(),
  ]);
  $child[2] = $this
    ->createCatalogTerm([
    'parent' => $parent
      ->id(),
  ]);

  // Create first product in this category.
  $product = [];
  $product[1] = $this
    ->createProduct([
    'taxonomy_catalog' => [
      $child[1]
        ->id(),
    ],
  ]);

  // Create second product in a different category.
  $product[2] = $this
    ->createProduct([
    'taxonomy_catalog' => [
      $child[2]
        ->id(),
    ],
  ]);

  // Categories are not expanded by default.
  $this
    ->drupalGet('');
  $assert
    ->pageTextContains($parent
    ->label());
  $assert
    ->linkExists($parent
    ->label() . ' (2)', 0, 'Product count is shown for top-level term.');
  $assert
    ->pageTextNotContains($child[1]
    ->label());
  $assert
    ->linkNotExists($child[1]
    ->label() . ' (1)', 0, 'Product count is not shown for child term.');
  $assert
    ->pageTextNotContains($child[2]
    ->label());
  $assert
    ->linkNotExists($child[2]
    ->label() . ' (1)', 0, 'Product count is not shown for child term.');

  // Turn on expanded term display.
  $this->block
    ->getPlugin()
    ->setConfigurationValue('expanded', TRUE);
  $this->block
    ->save();

  // @todo Catalog theme function doesn't use block configuration values,
  // it duplicates that information in the module config instead. This
  // needs to be fixed! (See also todo in CatalogBlock plugin.) But until
  // then, we need to set the config value here so that the link_title
  // functionality will work.
  \Drupal::configFactory()
    ->getEditable('uc_catalog.settings')
    ->set('expand_categories', TRUE)
    ->save();

  // Verify catalog is expanded to show all levels.
  $this
    ->drupalGet('');
  $assert
    ->pageTextContains($parent
    ->label());
  $assert
    ->linkExists($parent
    ->label() . ' (2)', 0, 'Product count is shown for top-level term.');
  $assert
    ->linkExists($child[1]
    ->label() . ' (1)', 0, 'Product count is shown for child term.');
  $assert
    ->linkExists($child[2]
    ->label() . ' (1)', 0, 'Product count is shown for child term.');
}