You are here

public function CatalogBlockTest::testProductCountDisplay in Ubercart 8.4

Tests display of product counts in catalog block.

File

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

Class

CatalogBlockTest
Tests the catalog block functionality.

Namespace

Drupal\Tests\uc_catalog\Functional

Code

public function testProductCountDisplay() {

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

  // Create a taxonomy term to use as a catalog category.
  $term = $this
    ->createCatalogTerm();
  $product = [];

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

  // Show product counts is the default.
  $this
    ->drupalGet('');
  $assert
    ->pageTextContains($term
    ->label());
  $assert
    ->linkExists($term
    ->label() . ' (1)', 0, 'Product count is shown.');

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

  // @todo Remove this when the CatalogBlock implements caching properly.
  Cache::invalidateTags($this->block
    ->getCacheTags());

  // Now there should be two products in this category.
  // This also tests the catalog block caching, because if caching isn't done
  // properly then we won't see the additional product here.
  $this
    ->drupalGet('');
  $assert
    ->pageTextContains($term
    ->label());
  $assert
    ->linkExists($term
    ->label() . ' (2)', 0, 'Product count of 2 is shown.');

  // Turn off product count display.
  $this->block
    ->getPlugin()
    ->setConfigurationValue('product_count', FALSE);
  $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('block_nodecount', FALSE)
    ->save();

  // Product count should no longer show.
  $this
    ->drupalGet('');
  $assert
    ->pageTextContains($term
    ->label());
  $assert
    ->linkExists($term
    ->label(), 0, 'Product count is not shown.');
}