View source
<?php
namespace Drupal\Tests\rate\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\rate\Traits\AssertRateWidgetTrait;
use Drupal\Tests\rate\Traits\NodeVoteTrait;
use Drupal\views\Tests\ViewTestData;
class ViewsFivestarWidgetTest extends WebDriverTestBase {
use NodeVoteTrait;
use AssertRateWidgetTrait;
protected $strictConfigSchema = FALSE;
protected $defaultTheme = 'stable';
public static $testViews = [
'test_views_widget',
];
public static $modules = [
'node',
'views',
'rate',
'rate_views_test',
];
protected $users;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
ViewTestData::createTestViews(get_class($this), [
'rate_views_test',
]);
NodeType::create([
'type' => 'article',
'name' => 'Article',
])
->save();
$this
->config('rate.settings')
->set('enabled_types_widgets.node', [
'article' => [
'widget_type' => 'fivestar',
],
])
->set('use_ajax', TRUE)
->save();
$this
->drupalCreateNode([
'type' => 'article',
'nid' => 1,
])
->save();
$this
->drupalCreateNode([
'type' => 'article',
'nid' => 2,
])
->save();
$permissions = [
'access content',
'cast rate vote on node of article',
];
$this->users[0] = $this
->createUser($permissions);
$this->users[1] = $this
->createUser($permissions);
}
public function testFivestarViewsWidget() {
$session = $this
->assertSession();
$this
->drupalLogin($this->users[0]);
$this
->drupalGet('test_views_widget');
$this
->assertFivestarById(1, 0);
$this
->voteFivestarById(1, 1);
$session
->assertWaitOnAjaxRequest();
$this
->assertFivestarById(1, 1);
$session
->linkExists('Undo');
$this
->assertFivestarById(2, 0);
$this
->voteFivestarById(2, 3);
$session
->assertWaitOnAjaxRequest();
$this
->assertFivestarById(2, 3);
$session
->linkExists('Undo');
$this
->drupalLogin($this->users[1]);
$this
->drupalGet('test_views_widget');
$this
->voteFivestarById(1, 5);
$session
->assertWaitOnAjaxRequest();
$this
->assertFivestarById(1, 3);
$session
->linkExists('Undo');
$this
->voteFivestarById(2, 4);
$session
->assertWaitOnAjaxRequest();
$this
->assertFivestarById(2, 3);
$session
->linkExists('Undo');
$this
->unVoteFivestarById(1);
$session
->assertWaitOnAjaxRequest();
$this
->assertFivestarById(1, 1);
$session
->linkExists('Undo');
$this
->unVoteFivestarById(2);
$session
->assertWaitOnAjaxRequest();
$this
->assertFivestarById(2, 3);
$session
->linkNotExists('Undo');
}
}