You are here

public function FivestarTest::testAuthorRating in Fivestar 8

Tests that authors can rate their own content.

File

tests/src/Functional/FivestarTest.php, line 77

Class

FivestarTest
Test base for the Fivestar module.

Namespace

Drupal\Tests\fivestar\Functional

Code

public function testAuthorRating() {
  $this
    ->drupalLogin($this->adminUser);

  // Add an author-rated fivestar field to the test_node_type content type.
  $this
    ->createFivestarField([
    'widget_type' => 'stars',
  ]);

  // Add the field to the display and form display.
  $this->displayRepository
    ->getViewDisplay('node', 'test_node_type', 'default')
    ->setComponent('fivestar_test')
    ->save();
  $this->displayRepository
    ->getFormDisplay('node', 'test_node_type', 'default')
    ->setComponent('fivestar_test')
    ->save();

  // Load the instance settings so we can set allow_ownvote.
  $instance = FieldConfig::load('node.test_node_type.fivestar_test');
  $instance
    ->setSetting('allow_ownvote', TRUE);
  $instance
    ->setSetting('rated_while', 'editing');
  $instance
    ->save();

  // Create a test_node_type node with a two-star rating.
  $edit = [
    'title[0][value]' => $this
      ->randomString(),
    // Equals a rating of 2 stars.
    'fivestar_test[0][rating]' => '40',
  ];
  $this
    ->drupalGet('node/add/test_node_type');
  $this
    ->submitForm($edit, 'Save');

  // Make sure the two-star rating shows on the node view.
  $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.');
}