View source
<?php
namespace Drupal\Tests\verf\Functional;
use Behat\Mink\Exception\ElementNotFoundException;
use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
class IntegrationTest extends BrowserTestBase {
use EntityReferenceTestTrait;
public static $modules = [
'verf_test_views',
];
private $nodeType;
private $view;
private $admin;
private $author;
public function setUp() {
parent::setUp();
$this->nodeType = $this
->drupalCreateContentType();
$this
->createEntityReferenceField('node', $this->nodeType
->id(), 'field_refs', 'Refs', 'node');
$this->admin = $this
->drupalCreateUser([], $this
->randomString(), true);
$this->author = $this
->drupalCreateUser([
'view own unpublished content',
]);
}
public function testFilterWorks() {
$referencedNode = $this
->drupalCreateNode([
'type' => $this->nodeType
->id(),
]);
$referencingNode = $this
->drupalCreateNode([
'type' => $this->nodeType
->id(),
'field_refs' => [
[
'target_id' => $referencedNode
->id(),
],
],
]);
$this
->drupalGet('verftest');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertPageContainsNodeTeaserWithText($referencedNode
->getTitle());
$this
->assertPageContainsNodeTeaserWithText($referencingNode
->getTitle());
$this
->assertSelectOptionCanBeSelected('Refs (VERF selector)', $referencedNode
->getTitle());
$this
->getSession()
->getPage()
->pressButton('Apply');
$this
->assertPageContainsNodeTeaserWithText($referencingNode
->getTitle());
$this
->assertPageNotContainsNodeTeaserWithText($referencedNode
->getTitle());
}
protected function assertPageContainsNodeTeaserWithText($text) {
try {
$this
->assertPageNotContainsNodeTeaserWithText($text);
} catch (\Exception $e) {
return;
}
throw new \Exception("No teaser could be found with the text: {$text}");
}
protected function assertPageNotContainsNodeTeaserWithText($text) {
$teasers = $this
->getSession()
->getPage()
->findAll('css', '.node--view-mode-teaser');
foreach ($teasers as $teaser) {
$this
->assertNotContains($text, $teaser
->getText());
}
}
protected function assertSelectOptionCanBeSelected($locator, $value) {
$this
->getSession()
->getPage()
->selectFieldOption($locator, $value);
}
protected function assertSelectOptionCanNotBeSelected($locator, $value) {
try {
$this
->getSession()
->getPage()
->selectFieldOption($locator, $value);
} catch (ElementNotFoundException $e) {
return;
}
throw new \AssertionError("{$value} could be selected in {$locator} while that should not be possible");
}
public function testRegression2720953() {
$published = $this
->drupalCreateNode([
'type' => $this->nodeType
->id(),
]);
$published
->setOwner($this->author)
->save();
$unpublished = $this
->drupalCreateNode([
'type' => $this->nodeType
->id(),
'status' => NodeInterface::NOT_PUBLISHED,
]);
$unpublished
->setOwner($this->author)
->save();
$referencingPublished = $this
->drupalCreateNode([
'type' => $this->nodeType
->id(),
'field_refs' => [
[
'target_id' => $published
->id(),
],
],
]);
$referencingUnpublished = $this
->drupalCreateNode([
'type' => $this->nodeType
->id(),
'field_refs' => [
[
'target_id' => $unpublished
->id(),
],
],
]);
$this
->drupalLogin($this->admin);
$this
->drupalGet('verftest');
$this
->assertSelectOptionCanBeSelected('Refs (VERF selector)', $published
->getTitle());
$this
->assertSelectOptionCanBeSelected('Refs (VERF selector)', $unpublished
->getTitle());
$this
->drupalLogin($this->author);
$this
->drupalGet('verftest');
$this
->assertSelectOptionCanBeSelected('Refs (VERF selector)', $published
->getTitle());
$this
->assertSelectOptionCanBeSelected('Refs (VERF selector)', $unpublished
->getTitle());
$this
->drupalLogout();
$this
->drupalGet('verftest');
$this
->assertSelectOptionCanBeSelected('Refs (VERF selector)', $published
->getTitle());
$this
->assertSelectOptionCanBeSelected('Refs (VERF selector)', '- Restricted access -');
$this
->assertSelectOptionCanNotBeSelected('Refs (VERF selector)', $unpublished
->getTitle());
}
}