domain_views_display-1.x-dev/tests/src/Functional/DomainViewsDisplayTest.php
tests/src/Functional/DomainViewsDisplayTest.php
<?php
declare(strict_types=1);
use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Element\NodeElement;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\domain\Traits\DomainTestTrait;
/**
* Provides tests for overriding views displays by domain.
*
* @group domain_views_display
*/
class DomainViewsDisplayTest extends BrowserTestBase {
use BlockCreationTrait;
use DomainTestTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'domain_views_display',
'domain_views_display_test',
'domain',
'node',
'views',
'block',
'text',
];
/**
* An array of domains for testing.
*
* @var array<string, \Drupal\domain\Entity\Domain>
*/
protected array $domains;
/**
* A node created for testing.
*/
protected NodeInterface $node;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->setBaseHostname();
$this->domainCreateTestDomains(3);
/** @var array<string, \Drupal\domain\Entity\Domain> $domains */
$domains = $this->getDomains();
$this->domains = $domains;
$this->drupalPlaceBlock('page_title_block');
$this->node = $this->drupalCreateNode();
}
/**
* Tests a page display being overridden.
*/
public function testPageOverrides(): void {
$this->getViewFromDomain('example_com', 'domain_views_display_test', 'page_overridden');
$this->assertSession()->titleEquals('Display page_overridden Title | Drupal');
$this->assertSame('Display page_overridden Title', $this->getH1()->getText());
$this->assertSession()->pageTextContains('This is the page_overridden display.');
$this->assertSession()->pageTextContains('page_overridden: ' . $this->node->label());
$this->getViewFromDomain('one_example_com', 'domain_views_display_test', 'page_overridden');
$this->assertSession()->titleEquals('Display page_override_block_one Title | Drupal');
$this->assertSame('Display page_override_block_one Title', $this->getH1()->getText());
$this->assertSession()->pageTextContains('This is the page_override_block_one display.');
$this->assertSession()->pageTextContains('page_override_block_one: ' . $this->node->label());
$this->getViewFromDomain('two_example_com', 'domain_views_display_test', 'page_overridden');
$this->assertSession()->titleEquals('Display page_override_page_two Title | Drupal');
$this->assertSame('Display page_override_page_two Title', $this->getH1()->getText());
$this->assertSession()->pageTextContains('This is the page_override_page_two display.');
$this->assertSession()->pageTextContains('page_override_page_two: ' . $this->node->label());
}
/**
* Tests a block display being overridden.
*/
public function testBlockOverrides(): void {
$this->drupalPlaceBlock('views_block:domain_views_display_test-block_overridden', [
'id' => 'domain-views-display-test',
]);
$this->drupalGet('<front>');
$this->assertSame('Display block_overridden Title', $this->getBlockTitleElement()->getText());
$this->assertSession()->pageTextContains('This is the block_overridden display.');
$this->assertSession()->pageTextContains('block_overridden: ' . $this->node->label());
$this->getFromDomain('one_example_com', '<front>');
$this->assertSame('Display block_override_block_one Title', $this->getBlockTitleElement()->getText());
$this->assertSession()->pageTextContains('This is the block_override_block_one display.');
$this->assertSession()->pageTextContains('block_override_block_one: ' . $this->node->label());
$this->getFromDomain('two_example_com', '<front>');
$this->assertSame('Display block_override_page_two Title', $this->getBlockTitleElement()->getText());
// @todo Fix below: the header doesn't seem to be displayed.
// $this->assertSession()->pageTextContains('This is the block_override_page_two display.');
$this->assertSession()->pageTextContains('block_override_page_two: ' . $this->node->label());
}
/**
* Tests a feed display being overridden.
*/
public function testFeedOverrides(): void {
// The feed_overridden display shows published nodes while the override
// shows unpublished.
$unpublished = $this->drupalCreateNode(['status' => FALSE]);
$this->getViewFromDomain('example_com', 'domain_views_display_test', 'feed_overridden');
$this->assertSame('Display feed_overridden Title', (string) $this->getFeedTitleElement());
$this->assertSession()->pageTextContains('feed_overridden: ' . $this->node->label());
$this->assertSession()->pageTextNotContains($unpublished->label());
$this->getViewFromDomain('one_example_com', 'domain_views_display_test', 'feed_overridden');
$this->assertSame('Display feed_override_feed_one Title', (string) $this->getFeedTitleElement());
$this->assertSession()->pageTextContains('feed_override_feed_one: ' . $unpublished->label());
$this->assertSession()->pageTextNotContains($this->node->label());
}
/**
* Tests a view executed programmatically.
*/
public function testDirectExecution(): void {
$this->drupalGet(Url::fromRoute('domain_views_display_test.direct_view'));
$this->assertSame('Display page_overridden Title', $this->getH1()->getText());
$this->assertSession()->pageTextContains('This is the page_overridden display.');
$this->assertSession()->pageTextContains('page_overridden: ' . $this->node->label());
$this->getFromDomain('one_example_com', Url::fromRoute('domain_views_display_test.direct_view'));
$this->assertSession()->pageTextContains('This is the page_override_block_one display.');
$this->assertSession()->pageTextContains('page_override_block_one: ' . $this->node->label());
$this->getFromDomain('two_example_com', Url::fromRoute('domain_views_display_test.direct_view'));
$this->assertSame('Display page_override_page_two Title', $this->getH1()->getText());
$this->assertSession()->pageTextContains('This is the page_override_page_two display.');
$this->assertSession()->pageTextContains('page_override_page_two: ' . $this->node->label());
}
/**
* Retrieves a path on a domain.
*
* @param string $domain_id
* The domain ID.
* @param string|\Drupal\Core\Url $path
* The path to get.
*/
protected function getFromDomain(string $domain_id, string|Url $path): void {
$this->assertArrayHasKey($domain_id, $this->domains);
$domain = $this->domains[$domain_id];
if ($path === '<front>') {
$path = Url::fromRoute($path);
}
$domain_path = $domain->buildUrl($path instanceof Url ? $path->toString() : $path);
$this->drupalGet($domain_path);
}
/**
* Retrieves a view page on a domain.
*
* @param string $domain_id
* The view ID.
* @param string $view_id
* The display ID.
* @param string $display_id
* The domain ID.
*/
protected function getViewFromDomain(string $domain_id, string $view_id, string $display_id): void {
$url = Url::fromRoute("view.$view_id.$display_id");
$this->getFromDomain($domain_id, $url);
}
/**
* Gets and asserts the H1 element.
*/
protected function getH1(): NodeElement {
$element = $this->getCurrentPage()->find('css', 'h1');
$this->assertNotNull($element);
return $element;
}
/**
* Gets and asserts the title element from the view block display.
*/
protected function getBlockTitleElement(): NodeElement {
$element = $this->getCurrentPage()->find('css', '#block-domain-views-display-test h2');
$this->assertNotNull($element);
return $element;
}
/**
* Gets and asserts the title element from the feed display.
*/
protected function getFeedTitleElement(): SimpleXMLElement {
$xml = new SimpleXMLElement($this->getCurrentPage()->getContent());
$element = $xml->channel?->title;
$this->assertNotNull($element);
return $element;
}
/**
* Gets the current page.
*
* @return \Behat\Mink\Element\DocumentElement
* The page document element.
*/
protected function getCurrentPage(): DocumentElement {
return $this->getSession()->getPage();
}
}
