View source
<?php
namespace Drupal\Tests\views_accordion\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class ViewsAccordionTest extends WebDriverTestBase {
use NodeCreationTrait;
public static $modules = [
'views_accordion_test',
];
protected $nodes = [];
protected $user1;
protected $user2;
protected function setUp() {
parent::setUp();
$this->user1 = $this
->createUser();
$this->user2 = $this
->createUser();
$this->nodes[] = $this
->createNode([
'uid' => $this->user1
->id(),
]);
$this->nodes[] = $this
->createNode([
'uid' => $this->user1
->id(),
]);
$this->nodes[] = $this
->createNode([
'uid' => $this->user2
->id(),
]);
$this->nodes[] = $this
->createNode([
'uid' => $this->user2
->id(),
]);
}
public function testViewsAccordion() {
$this
->drupalGet('views-accordion-test');
$driver = $this
->getSession()
->getDriver();
$settings = $this
->getDrupalSettings();
$this
->assertArrayHasKey('views_accordion', $settings, 'Views accordion JS settings avaialable');
$driver
->isVisible($this
->cssSelectToXpath('#ui-id-2'));
$this
->assertFalse($driver
->isVisible($this
->cssSelectToXpath('#ui-id-4')), 'Row two is collapsed');
$this
->click('#ui-id-1');
$driver
->isVisible($this
->cssSelectToXpath('#ui-id-2'));
$driver
->isVisible($this
->cssSelectToXpath('#ui-id-1 span.ui-icon-triangle-1-s'));
$driver
->isVisible($this
->cssSelectToXpath('#ui-id-3 span.ui-icon-triangle-1-e'));
$this
->drupalGet('views-accordion-test-grouping');
$this
->assertEquals($this->user1
->getAccountName(), $driver
->getText($this
->cssSelectToXpath('#ui-id-1')));
$first_group_xpath = $this
->cssSelectToXpath('#ui-id-2');
$driver
->isVisible($first_group_xpath);
$first_group_content = $driver
->getText($first_group_xpath);
$this
->assertContains($this->nodes[0]
->getTitle(), $first_group_content, 'First node is on first accordion group');
$this
->assertContains($this->nodes[1]
->getTitle(), $first_group_content, 'Second node is on first accordion group');
$this
->assertNotContains($this->nodes[2]
->getTitle(), $first_group_content, 'Third node is not on first accordion group');
$this
->assertNotContains($this->nodes[3]
->getTitle(), $first_group_content, 'Fourth node is not on first accordion group');
$this
->assertEquals($this->user2
->getAccountName(), $driver
->getText($this
->cssSelectToXpath('#ui-id-3')));
$second_group_xpath = $this
->cssSelectToXpath('#ui-id-4');
$this
->assertFalse($driver
->isVisible($second_group_xpath), 'Second accordion group is collapsed');
$this
->click('#ui-id-3');
$driver
->isVisible($second_group_xpath);
$second_group_content = $driver
->getText($second_group_xpath);
$this
->assertNotContains($this->nodes[0]
->getTitle(), $second_group_content, 'First node is not on second accordion group');
$this
->assertNotContains($this->nodes[1]
->getTitle(), $second_group_content, 'Second node is not on second accordion group');
$this
->assertContains($this->nodes[2]
->getTitle(), $second_group_content, 'Third node is on second accordion group');
$this
->assertContains($this->nodes[3]
->getTitle(), $second_group_content, 'Fourth node is on second accordion group');
}
}