View source
<?php
namespace Drupal\Tests\uc_catalog\Functional;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\Cache;
class CatalogBlockTest extends CatalogTestBase {
protected static $modules = [
'uc_catalog',
'block',
];
protected static $adminPermissions = [
'administer catalog',
'view catalog',
'administer blocks',
];
protected $block;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this->block = $this
->drupalPlaceBlock('uc_catalog_block');
}
public function testCatalogBlock() {
$assert = $this
->assertSession();
$configuration = $this->block
->getPlugin()
->getConfiguration();
$this
->assertFalse($configuration['link_title']);
$this
->assertFalse($configuration['expanded']);
$this
->assertTrue($configuration['product_count']);
$this
->assertEquals(BlockPluginInterface::BLOCK_LABEL_VISIBLE, $configuration['label_display']);
$term = $this
->createCatalogTerm();
$product = $this
->createProduct([
'taxonomy_catalog' => [
$term
->id(),
],
]);
$this
->drupalGet('');
$assert
->pageTextContains($this->block
->label());
$assert
->pageTextContains($term
->label());
$assert
->linkExists($term
->label() . ' (1)', 0, 'The category is listed in the catalog block.');
$this
->clickLink($term
->label() . ' (1)');
$assert
->titleEquals($term
->label() . ' | Drupal');
$assert
->linkExists($product
->label(), 0, 'The product is listed in the catalog.');
}
public function testTitleLink() {
$assert = $this
->assertSession();
$this
->drupalGet('');
$assert
->pageTextContains($this->block
->label());
$assert
->linkNotExists($this->block
->label(), 0, 'The block title is not a link.');
$assert
->linkByHrefNotExists('catalog', 0, 'The block title is not linked to the catalog page.');
$this->block
->getPlugin()
->setConfigurationValue('link_title', TRUE);
$this->block
->save();
$this
->drupalGet('');
$assert
->linkExists($this->block
->label(), 0, 'The block title is a link.');
$assert
->linkByHrefExists('catalog', 0, 'The block title is linked to the catalog page.');
}
public function testExpandCategories() {
$assert = $this
->assertSession();
$parent = $this
->createCatalogTerm();
$child = [];
$child[1] = $this
->createCatalogTerm([
'parent' => $parent
->id(),
]);
$child[2] = $this
->createCatalogTerm([
'parent' => $parent
->id(),
]);
$product = [];
$product[1] = $this
->createProduct([
'taxonomy_catalog' => [
$child[1]
->id(),
],
]);
$product[2] = $this
->createProduct([
'taxonomy_catalog' => [
$child[2]
->id(),
],
]);
$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.');
$this->block
->getPlugin()
->setConfigurationValue('expanded', TRUE);
$this->block
->save();
\Drupal::configFactory()
->getEditable('uc_catalog.settings')
->set('expand_categories', TRUE)
->save();
$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.');
}
public function testProductCountDisplay() {
$assert = $this
->assertSession();
$term = $this
->createCatalogTerm();
$product = [];
$product[] = $this
->createProduct([
'taxonomy_catalog' => [
$term
->id(),
],
]);
$this
->drupalGet('');
$assert
->pageTextContains($term
->label());
$assert
->linkExists($term
->label() . ' (1)', 0, 'Product count is shown.');
$product[] = $this
->createProduct([
'taxonomy_catalog' => [
$term
->id(),
],
]);
Cache::invalidateTags($this->block
->getCacheTags());
$this
->drupalGet('');
$assert
->pageTextContains($term
->label());
$assert
->linkExists($term
->label() . ' (2)', 0, 'Product count of 2 is shown.');
$this->block
->getPlugin()
->setConfigurationValue('product_count', FALSE);
$this->block
->save();
\Drupal::configFactory()
->getEditable('uc_catalog.settings')
->set('block_nodecount', FALSE)
->save();
$this
->drupalGet('');
$assert
->pageTextContains($term
->label());
$assert
->linkExists($term
->label(), 0, 'Product count is not shown.');
}
}