FormatterRedirectTest.php in Field Redirection 8.2
File
tests/src/Functional/FormatterRedirectTest.php
View source
<?php
namespace Drupal\Tests\field_redirection\Functional;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_redirection\Traits\FieldRedirectionTestTrait;
class FormatterRedirectTest extends BrowserTestBase {
use FieldRedirectionTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'field_ui',
'field_redirection',
];
protected $testContentType;
protected function setUp() {
parent::setUp();
$this->testContentType = $this
->setupContentTypeAndField();
}
public function testFieldRedirection() {
EntityViewDisplay::create([
'targetEntityType' => 'node',
'bundle' => $this->testContentType
->id(),
'mode' => 'full',
'status' => TRUE,
])
->setComponent('url', [
'type' => 'field_redirection_formatter',
])
->save();
$redirectTo = Node::create([
'type' => $this->testContentType
->id(),
'title' => $this
->randomMachineName(),
'status' => 1,
]);
$redirectTo
->save();
$node = Node::create([
'type' => $this->testContentType
->id(),
'title' => $this
->randomMachineName(),
'url' => [
[
'uri' => 'entity:node/' . $redirectTo
->id(),
],
],
]);
$node
->save();
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->addressEquals('node/' . $redirectTo
->id());
$this
->assertSession()
->pageTextNotContains('This page is set to redirect to');
$this
->drupalLogin($this
->createUser([
'bypass redirection',
]));
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->addressEquals('node/' . $node
->id());
$this
->assertSession()
->pageTextContains('This page is set to redirect to');
$this
->getSession()
->getPage()
->clickLink('another URL');
$this
->assertSession()
->addressEquals('node/' . $redirectTo
->id());
}
}