You are here

fivestar.field.test in Fivestar 7.2

Same filename and directory in other branches
  1. 8 test/fivestar.field.test

Simpletests for the Fivestar module.

File

test/fivestar.field.test
View source
<?php

/**
 * @file
 * Simpletests for the Fivestar module.
 */

/**
 * Makes sure fivestar widgets can be created and used.
 */
class FivestarTestCase extends FivestarBaseTestCase {

  /**
   * Provides test metadata.
   */
  public static function getInfo() {
    return array(
      'name' => 'Fivestar widgets',
      'description' => 'Make sure fivestar widgets can be created and used.',
      'group' => 'Fivestar',
    );
  }

  /**
   * Test that authors can rate their own content.
   */
  public function testAuthorRating() {
    $this
      ->drupalLogin($this->adminUser);

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

    // Load the instance settings so we can set allow_ownvote.
    $instance = field_read_instance('node', 'fivestar_test', 'test_node_type');
    $instance['settings']['allow_ownvote'] = 1;
    field_update_instance($instance);

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

    // Make sure the two-star rating shows on the node view.
    $result = $this
      ->xpath("//div[contains(@class, 'field-name-fivestar-test')]//div[contains(@class,'star-first')]/span");
    $this
      ->assertEqual($result[0][0], '2', 'Content authors can rate their own content using the stars widget.');
  }

  /**
   * Test that users can rate content with exposed widgets.
   */
  public function testViewerRating() {
    $this
      ->createFivestarField(array(
      'widget_type' => 'exposed',
    ));

    // Add a test_node_type to rate.
    $node = $this
      ->drupalCreateNode(array(
      'type' => 'test_node_type',
    ));
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->drupalLogin($this->voterUser);

    // Rate the test_node_type.
    $edit = array(
      'vote' => '60',
    );
    $this
      ->drupalPost('node/' . $node->nid, $edit, 'Rate');
    $this
      ->assertNoRaw('No votes yet', 'Visitors can rate content using the exposed widget.');

    // Load the instance settings so we can change certain settings.
    $instance = field_read_instance('node', 'fivestar_test', 'test_node_type');

    // Let's see if visitors is able to re-vote.
    // @see http://drupal.org/node/356605
    $instance['settings']['allow_revote'] = 1;
    field_update_instance($instance);
    $this
      ->drupalGet('node/' . $node->nid);
    $result = $this
      ->xpath("//div[contains(@class, 'field-name-fivestar-test')]//select[contains(@name,'vote')]");
    $this
      ->assertEqual(count($result), TRUE, 'Visitors can re-vote');

    // Let's test to make sure the cancel option is not available if disabled.
    // @see http://drupal.org/node/1269276
    $this
      ->assertNoRaw('Cancel rating', 'User cannot cancel his vote.');
    $instance['settings']['allow_clear'] = 1;
    field_update_instance($instance);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertRaw('Cancel rating', 'User can cancel his vote.');

    // Now let's change the field to have exposed off and make sure the value
    // is still there.
    // @see http://drupal.org/node/1242082
    $instance['display']['default']['settings']['expose'] = FALSE;
    field_update_instance($instance);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertFalse($this
      ->xpath("//form[contains(@class, 'fivestar-widget')]"));

    // Make sure the three-star rating still shows on the node view.
    $result = $this
      ->xpath("//div[contains(@class, 'field-name-fivestar-test')]//div[contains(@class,'star-first')]/span");
    $this
      ->assertEqual($result[0][0], '3', 'The static display still shows three stars.');
  }

  /**
   * Test exposed display setting.
   *
   * Tests that users can not rate content with exposed widgets that have their
   * exposed display setting set to FALSE.
   */
  public function testViewerNonRating() {

    // Add an exposed field, with the Exposed display settings set to FALSE.
    $this
      ->createFivestarField(array(
      'widget_type' => 'exposed',
      'display' => array(
        'default' => array(
          'type' => 'fivestar_formatter_default',
          'settings' => array(
            'style' => 'average',
            'text' => 'average',
            'expose' => FALSE,
          ),
        ),
      ),
    ));

    // Add a test_node_type to test static widget.
    $node = $this
      ->drupalCreateNode(array(
      'type' => 'test_node_type',
    ));

    // Rate the test_node_type.
    $this
      ->drupalLogin($this->voterUser);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertRaw('No votes yet', 'Fivestar field has no votes.');
    $this
      ->assertFalse($this
      ->xpath("//form[contains(@class, 'fivestar-widget')]"));
  }

  /**
   * Test that users can rate content with exposed widgets.
   */
  public function testViewerRatingAjax() {

    // Add a viewer-rated fivestar field to the test_node_type content type.
    $this
      ->createFivestarField(array(
      'widget_type' => 'exposed',
    ));

    // Add a test_node_type to rate.
    $node = $this
      ->drupalCreateNode(array(
      'type' => 'test_node_type',
    ));

    // Rate the test_node_type.
    $this
      ->drupalLogin($this->voterUser);
    $edit = array(
      'vote' => '60',
    );
    $commands = $this
      ->drupalPostAJAX('node/' . $node->nid, $edit, "vote", NULL, array(), array(), "fivestar-custom-widget");
    $expected = array(
      'command' => 'fivestarUpdate',
    );
    $this
      ->assertCommand($commands, $expected, "The fivestarUpdate AJAX command was returned.");
  }

  /**
   * Tests changing the fivestar widget.
   *
   * Test that we can switch the fivestar widgets around for the exposed
   * widget type.
   */
  public function testExposedWidgetDisplay() {

    // Let's add an exposed widget but display the static widget.
    // It's simpler to compare the display type using the static widget.
    $this
      ->createFivestarField(array(
      'widget_type' => 'exposed',
      'display' => array(
        'default' => array(
          'type' => 'fivestar_formatter_default',
          'settings' => array(
            'style' => 'average',
            'text' => 'average',
            'expose' => FALSE,
          ),
        ),
      ),
    ));
    $instance = field_read_instance('node', 'fivestar_test', 'test_node_type');

    // Add a test_node_type to test widget against.
    $node = $this
      ->drupalCreateNode(array(
      'type' => 'test_node_type',
    ));

    // Test the Default Widget.
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertTrue($this
      ->xpath("//div[contains(@class, 'fivestar-default')]//div[contains(@class,'star-first')]/span"));
    $widgets = module_invoke_all('fivestar_widgets');
    foreach ($widgets as $path => $name) {
      $instance['display']['default']['settings']['widget']['fivestar_widget'] = $path;
      field_update_instance($instance);
      $widget_class = 'fivestar-' . drupal_strtolower($name);
      $this
        ->drupalGet('node/' . $node->nid);
      $result = $this
        ->xpath("//div[contains(@class, '" . $widget_class . "')]//div[contains(@class,'star-first')]/span");
      $this
        ->assertEqual($result[0][0], '0', "The {$name} fivestar widget is properly displayed.");
    }
  }

}

Classes

Namesort descending Description
FivestarTestCase Makes sure fivestar widgets can be created and used.