listjs-8.x-1.x-dev/modules/listjs_views/tests/src/FunctionalJavascript/ListjsViewsTest.php
modules/listjs_views/tests/src/FunctionalJavascript/ListjsViewsTest.php
<?php
namespace Drupal\Tests\listjs_views\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
/**
* Tests listjs views style plugin.
*
* @group listjs_views
*/
class ListjsViewsTest extends WebDriverTestBase {
use NodeCreationTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'listjs',
'listjs_cdn_alter',
'listjs_views',
'listjs_test_views',
];
/**
* The installation profile to use with this test.
*
* @var string
*/
protected $profile = 'standard';
/**
* Test contents.
*
* @var array
*/
private $contents = [
[
'title' => 'Max',
'body' => 'The brave cat',
],
[
'title' => 'Chloe',
'body' => 'The cute cat',
],
[
'title' => 'Angel',
'body' => 'The holy cat',
],
[
'title' => 'Smokey',
'body' => 'The wispy cat',
],
[
'title' => 'Felix',
'body' => 'The lucky cat',
],
[
'title' => 'Mimi',
'body' => 'The chinese cat',
],
[
'title' => 'Mau',
'body' => 'The indian cat',
],
[
'title' => 'Grumpy',
'body' => 'The grumpy cat',
],
[
'title' => 'Sylvester',
'body' => 'The Looney Tunes cat',
],
[
'title' => 'Tom',
'body' => 'The Tom and Jerry cat',
],
];
/**
* Randomly chosen index of test content.
*
* @var int
*/
private $randomContent;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
foreach ($this->contents as $item) {
$this->drupalCreateNode([
'title' => $item['title'],
'body' => [
'value' => $item['body'],
'format' => filter_default_format(),
],
]);
}
$this->randomContent = mt_rand(0, count($this->contents) - 1);
}
/**
* Tests whether filter is working for title field.
*/
public function testFilterTitleField() {
$this->drupalGet('/listjs-views-test');
$page = $this->getSession()->getPage();
$page->fillField('listjs_test_views-page_1-wrapper-filter', $this->contents[$this->randomContent]['title']);
$this->assertCount(1, $page->findAll('css', '.view-listjs-test-views li'));
$this->assertEquals($page->find('css', '.view-listjs-test-views li .views-field-title')->getText(), $this->contents[$this->randomContent]['title']);
$this->assertEquals($page->find('css', '.view-listjs-test-views li .views-field-body')->getText(), $this->contents[$this->randomContent]['body']);
}
/**
* Tests whether filter is working for body field.
*/
public function testFilterBodyField() {
$this->drupalGet('/listjs-views-test');
$page = $this->getSession()->getPage();
$page->fillField('listjs_test_views-page_1-wrapper-filter', $this->contents[$this->randomContent]['body']);
$this->assertCount(1, $page->findAll('css', '.view-listjs-test-views li'));
$this->assertEquals($page->find('css', '.view-listjs-test-views li .views-field-body')->getText(), $this->contents[$this->randomContent]['body']);
$this->assertEquals($page->find('css', '.view-listjs-test-views li .views-field-title')->getText(), $this->contents[$this->randomContent]['title']);
}
/**
* Tests whether sort is working.
*/
public function testSort() {
$this->drupalGet('/listjs-views-test');
$page = $this->getSession()->getPage();
$content_count = count($this->contents);
$page->findButton('Content: Title sort')->click();
$this->assertCount($content_count, $page->findAll('css', '.view-listjs-test-views li'));
$elements = $page->findAll('css', '.view-listjs-test-views li .views-field-title');
$this->assertEquals($elements[0]->getText(), 'Angel');
$this->assertEquals($elements[$content_count - 1]->getText(), 'Tom');
}
/**
* Tests whether there are no result for non existing value.
*/
public function testNoResult() {
$this->drupalGet('/listjs-views-test');
$page = $this->getSession()->getPage();
$page->fillField('listjs_test_views-page_1-wrapper-filter', 'Octopuss');
$this->assertEmpty($page->findAll('css', '.view-listjs-test-views li'));
}
}
