View source
<?php
namespace Drupal\Tests\fivestar\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\fivestar\Traits\FivestarTestTrait;
class FivestarTest extends BrowserTestBase {
use FivestarTestTrait;
protected static $modules = [
'node',
'fivestar',
'votingapi',
];
protected $defaultTheme = 'classy';
protected $displayRepository;
protected $adminUser;
protected $voterUser;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'test_node_type',
'name' => 'Rated content type',
]);
$this->displayRepository = \Drupal::service('entity_display.repository');
$this->adminUser = $this
->createUser([
'create test_node_type content',
'edit any test_node_type content',
'rate content',
]);
$this->voterUser = $this
->createUser([
'rate content',
]);
}
public function testAuthorRating() {
$this
->drupalLogin($this->adminUser);
$this
->createFivestarField([
'widget_type' => 'stars',
]);
$this->displayRepository
->getViewDisplay('node', 'test_node_type', 'default')
->setComponent('fivestar_test')
->save();
$this->displayRepository
->getFormDisplay('node', 'test_node_type', 'default')
->setComponent('fivestar_test')
->save();
$instance = FieldConfig::load('node.test_node_type.fivestar_test');
$instance
->setSetting('allow_ownvote', TRUE);
$instance
->setSetting('rated_while', 'editing');
$instance
->save();
$edit = [
'title[0][value]' => $this
->randomString(),
'fivestar_test[0][rating]' => '40',
];
$this
->drupalGet('node/add/test_node_type');
$this
->submitForm($edit, 'Save');
$elements = $this
->xpath("//div[contains(@class, 'field--name-fivestar-test')]//div[contains(@class,'star-first')]/span");
$this
->assertEquals('2', $elements[0]
->getText(), 'Content authors can rate their own content using the stars widget.');
}
public function testViewerNonRating() {
$this
->createFivestarField([
'widget_type' => 'exposed',
'display' => [
'default' => [
'type' => 'fivestar_stars',
'settings' => [
'style' => 'average',
'text' => 'average',
'expose' => FALSE,
],
],
],
]);
$this->displayRepository
->getViewDisplay('node', 'test_node_type', 'default')
->setComponent('fivestar_test')
->save();
$this->displayRepository
->getFormDisplay('node', 'test_node_type', 'default')
->setComponent('fivestar_test')
->save();
$node = $this
->createNode([
'type' => 'test_node_type',
]);
$this
->drupalLogin($this->voterUser);
$this
->drupalGet('node/' . $node
->id());
$this
->assertRaw('No votes yet', 'Fivestar field has no votes.');
$this
->assertEmpty($this
->xpath("//form[contains(@class, 'fivestar-widget')]"));
$edit = [
'title[0][value]' => $this
->randomMachineName(),
];
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('has been updated.');
}
}