You are here

trait AssertRateWidgetTrait in Rate 8

Same name and namespace in other branches
  1. 8.2 tests/src/Traits/AssertRateWidgetTrait.php \Drupal\Tests\rate\Traits\AssertRateWidgetTrait

Assert methods to verify results of voting for a single node.

This trait is meant to be used only by Functional and FunctionalJavascript test classes.

Hierarchy

6 files declare their use of AssertRateWidgetTrait
NodeMultipleWidgetTest.php in tests/src/Functional/NodeMultipleWidgetTest.php
NodeMultipleWidgetTest.php in tests/src/FunctionalJavascript/NodeMultipleWidgetTest.php
RateJavascriptNodeWidgetTestBase.php in tests/src/FunctionalJavascript/RateJavascriptNodeWidgetTestBase.php
RateMultilingualTest.php in tests/src/Functional/RateMultilingualTest.php
RateNodeWidgetTestBase.php in tests/src/Functional/RateNodeWidgetTestBase.php

... See full list

File

tests/src/Traits/AssertRateWidgetTrait.php, line 11

Namespace

Drupal\Tests\rate\Traits
View source
trait AssertRateWidgetTrait {

  /**
   * Assert "Fivestar".
   *
   * @param int $expected
   *   The expected number of stars (from 0 to 5).
   */
  public function assertFivestar($expected) {
    $session = $this
      ->assertSession();
    $session
      ->elementExists('css', '.rate-widget-fivestar');
    if ($expected > 0) {
      $session
        ->elementExists('css', '.rate-fivestar-btn-filled.rate-fivestar-' . $expected);
    }
    if ($expected < 5) {
      $session
        ->elementExists('css', '.rate-fivestar-btn-empty.rate-fivestar-' . ++$expected);
    }
  }

  /**
   * Assert "Fivestar" by node id.
   *
   * @param int $nid
   *   The node id.
   * @param int $expected
   *   The expected number of stars (from 0 to 5).
   */
  protected function assertFivestarById($nid, $expected) {
    $session = $this
      ->assertSession();
    $node_selector = '[data-drupal-selector="rate-node-' . $nid . '"]';
    $session
      ->elementExists('css', '.rate-fivestar-1');
    if ($expected > 0) {
      $session
        ->elementExists('css', $node_selector . ' .rate-fivestar-btn-filled.rate-fivestar-' . $expected);
    }
    if ($expected < 5) {
      $session
        ->elementExists('css', $node_selector . ' .rate-fivestar-btn-empty.rate-fivestar-' . ++$expected);
    }
  }

  /**
   * Assert "Number Up / Down".
   *
   * @param string $expected
   *   The expected result of voting (e.g. '-1' or '+2').
   */
  public function assertNumberUpDown($expected) {
    $session = $this
      ->assertSession();
    $session
      ->elementExists('css', '.rate-widget-number-up-down');
    $session
      ->elementTextContains('css', '.rate-number-up-down-rating', $expected);
  }

  /**
   * Assert "Thumbs Up / Down".
   *
   * @param int $expected_up
   *   The expected result of voting for 'Up'. Result in percent (e.g. 67).
   * @param int $expected_down
   *   The expected result of voting for 'Down'. Result in percent (e.g. 33).
   */
  public function assertThumbsUpDown($expected_up, $expected_down) {
    $session = $this
      ->assertSession();
    $session
      ->elementExists('css', '.rate-widget-thumbs-up-down');
    $session
      ->elementTextContains('css', '.thumb-up', $expected_up . '%');
    $session
      ->elementTextContains('css', '.thumb-down', $expected_down . '%');
  }

  /**
   * Assert "Thumbs Up".
   *
   * @param int $expected
   *   The expected result of voting. Sum of votes (e.g. 2).
   */
  public function assertThumbsUp($expected) {
    $session = $this
      ->assertSession();
    $session
      ->elementExists('css', '.rate-widget-thumbs-up');
    $session
      ->elementTextContains('css', '.rate-score', $expected);
  }

  /**
   * Assert "YesNo".
   *
   * @param int $expected_yes
   *   The expected result of voting for 'Yes'. Sum of votes (e.g. 6).
   * @param int $expected_no
   *   The expected result of voting for 'No'. Sum of votes (e.g. 2).
   */
  public function assertYesNo($expected_yes, $expected_no) {
    $session = $this
      ->assertSession();
    $session
      ->elementExists('css', '.rate-widget-yesno');
    $session
      ->elementTextContains('css', '.rate-yes', $expected_yes);
    $session
      ->elementTextContains('css', '.rate-no', $expected_no);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertRateWidgetTrait::assertFivestar public function Assert "Fivestar".
AssertRateWidgetTrait::assertFivestarById protected function Assert "Fivestar" by node id.
AssertRateWidgetTrait::assertNumberUpDown public function Assert "Number Up / Down".
AssertRateWidgetTrait::assertThumbsUp public function Assert "Thumbs Up".
AssertRateWidgetTrait::assertThumbsUpDown public function Assert "Thumbs Up / Down".
AssertRateWidgetTrait::assertYesNo public function Assert "YesNo".