Loading config/schema/term_condition.schema.yml 0 → 100644 +10 −0 Original line number Diff line number Diff line condition.plugin.term: type: condition.plugin mapping: tid: type: sequence sequence: type: mapping mapping: target_id: type: string tests/src/Functional/TermConditionTest.php 0 → 100644 +121 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\term_condition\Functional; use Drupal\Component\Utility\Html; use Drupal\block\Entity\Block; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Url; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\VocabularyInterface; use Drupal\Tests\block\Functional\BlockTestBase; use Drupal\Tests\field\Traits\EntityReferenceTestTrait; use Drupal\Tests\node\Traits\ContentTypeCreationTrait; use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\Tests\RandomGeneratorTrait; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; /** * Tests basic block functionality. * * @group block */ class TermConditionTest extends BlockTestBase { use RandomGeneratorTrait; use ContentTypeCreationTrait; use EntityReferenceTestTrait; use NodeCreationTrait { createNode as drupalCreateNode; } protected static $modules = [ 'block', 'filter', 'test_page_test', 'help', 'block_test', 'taxonomy', 'term_condition', 'node', ]; /** * {@inheritdoc} */ protected $defaultTheme = 'classy'; /** * @var TermInterface */ protected $term; /** * @var VocabularyInterface */ protected $vocab; protected $nodeWithTerm; protected $nodeWithoutTerm; protected function setUp(): void { parent::setUp(); // @todo - Replace this with the VocabCreate trait $this->vocab = Vocabulary::create([ 'vid' => 'tags', 'name' => $this->randomString(), ]); $this->vocab->save(); // @todo - Replace this with the TermCreate trait $this->term = Term::create([ 'vid' => $this->vocab->id(), 'name' => 'sample' ]); $this->term->save(); $this->drupalCreateContentType(['type' => 'article']); $this->createEntityReferenceField('node', 'article', 'field_tags', NULL, 'taxonomy_term'); // Create a node with and without the term to review later. $this->nodeWithTerm = $this->drupalCreateNode(['type' => 'article', 'field_tags' => ['target_id' => $this->term->id()]]); $this->nodeWithoutTerm = $this->drupalCreateNode(['type' => 'article']); } /** * Tests block visibility. */ public function testBlockVisibility() { $block_name = 'system_powered_by_block'; // Create a random title for the block. $title = $this->randomMachineName(8); $default_theme = $this->config('system.theme')->get('default'); $edit = [ 'id' => strtolower($this->randomMachineName(8)), 'region' => 'sidebar_first', 'settings[label]' => $title, 'settings[label_display]' => TRUE, ]; // Set the block to be shown when the node has the term. $edit['visibility[term][tid]'] = $this->term->getName() . ' (' . $this->term->id() . ')'; $edit['visibility[term][context_mapping][node]'] = '@node.node_route_context:node'; $this->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme); $this->submitForm($edit, 'Save block'); $this->assertSession()->pageTextContains('The block configuration has been saved'); // Check the block renders on the node with the term. $this->drupalGet($this->nodeWithTerm->toUrl('canonical')); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextContains('Powered by Drupal'); // Check the block doesn't render on the node without the term. $this->drupalGet($this->nodeWithoutTerm->toUrl('canonical')); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextNotContains('Powered by Drupal'); // Check the block doesn't render on the homepage. $this->drupalGet('<front>'); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextNotContains('Powered by Drupal'); } } Loading
config/schema/term_condition.schema.yml 0 → 100644 +10 −0 Original line number Diff line number Diff line condition.plugin.term: type: condition.plugin mapping: tid: type: sequence sequence: type: mapping mapping: target_id: type: string
tests/src/Functional/TermConditionTest.php 0 → 100644 +121 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\term_condition\Functional; use Drupal\Component\Utility\Html; use Drupal\block\Entity\Block; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Url; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\VocabularyInterface; use Drupal\Tests\block\Functional\BlockTestBase; use Drupal\Tests\field\Traits\EntityReferenceTestTrait; use Drupal\Tests\node\Traits\ContentTypeCreationTrait; use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\Tests\RandomGeneratorTrait; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; /** * Tests basic block functionality. * * @group block */ class TermConditionTest extends BlockTestBase { use RandomGeneratorTrait; use ContentTypeCreationTrait; use EntityReferenceTestTrait; use NodeCreationTrait { createNode as drupalCreateNode; } protected static $modules = [ 'block', 'filter', 'test_page_test', 'help', 'block_test', 'taxonomy', 'term_condition', 'node', ]; /** * {@inheritdoc} */ protected $defaultTheme = 'classy'; /** * @var TermInterface */ protected $term; /** * @var VocabularyInterface */ protected $vocab; protected $nodeWithTerm; protected $nodeWithoutTerm; protected function setUp(): void { parent::setUp(); // @todo - Replace this with the VocabCreate trait $this->vocab = Vocabulary::create([ 'vid' => 'tags', 'name' => $this->randomString(), ]); $this->vocab->save(); // @todo - Replace this with the TermCreate trait $this->term = Term::create([ 'vid' => $this->vocab->id(), 'name' => 'sample' ]); $this->term->save(); $this->drupalCreateContentType(['type' => 'article']); $this->createEntityReferenceField('node', 'article', 'field_tags', NULL, 'taxonomy_term'); // Create a node with and without the term to review later. $this->nodeWithTerm = $this->drupalCreateNode(['type' => 'article', 'field_tags' => ['target_id' => $this->term->id()]]); $this->nodeWithoutTerm = $this->drupalCreateNode(['type' => 'article']); } /** * Tests block visibility. */ public function testBlockVisibility() { $block_name = 'system_powered_by_block'; // Create a random title for the block. $title = $this->randomMachineName(8); $default_theme = $this->config('system.theme')->get('default'); $edit = [ 'id' => strtolower($this->randomMachineName(8)), 'region' => 'sidebar_first', 'settings[label]' => $title, 'settings[label_display]' => TRUE, ]; // Set the block to be shown when the node has the term. $edit['visibility[term][tid]'] = $this->term->getName() . ' (' . $this->term->id() . ')'; $edit['visibility[term][context_mapping][node]'] = '@node.node_route_context:node'; $this->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme); $this->submitForm($edit, 'Save block'); $this->assertSession()->pageTextContains('The block configuration has been saved'); // Check the block renders on the node with the term. $this->drupalGet($this->nodeWithTerm->toUrl('canonical')); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextContains('Powered by Drupal'); // Check the block doesn't render on the node without the term. $this->drupalGet($this->nodeWithoutTerm->toUrl('canonical')); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextNotContains('Powered by Drupal'); // Check the block doesn't render on the homepage. $this->drupalGet('<front>'); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextNotContains('Powered by Drupal'); } }