'Fivestar widgets', 'description' => 'Make sure fivestar widgets can be created and used.', 'group' => 'Fivestar', ); } public function setUp() { parent::setUp(array('fivestar')); $this->admin_user = $this->drupalCreateUser(array('create article content', 'rate content')); $this->voter = $this->drupalCreateUser(array('rate content')); } /** * Test that authors can rate their own content. */ public function testAuthorRating() { $this->drupalLogin($this->admin_user); // Add an author-rated fivestar field to the article content type. $this->createFivestarField(array('widget_type' => 'stars')); // Save an article node with a two-star rating. $edit = array( 'title' => $this->randomString(), 'fivestar_test[und][0][rating]' => '40' // Equals a rating of 2 stars. ); $this->drupalPost('node/add/article', $edit, t('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() { // Add a viewer-rated fivestar field to the article content type. $this->createFivestarField(array('widget_type' => 'exposed')); // Add an article to rate. $node = $this->drupalCreateNode(array('type' => 'article')); // Rate the article. $this->drupalLogin($this->voter); $edit = array( 'vote' => '60', ); $this->drupalPost('node/' . $node->nid, $edit, t('Rate')); $this->assertNoRaw(t('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', 'article'); // Lets test to make sure the cancel option is not available if disabled. // @see http://drupal.org/node/1269276 $this->assertNoRaw(t('Cancel rating'), 'User cannot cancel his vote.'); $instance['settings']['allow_clear'] = 1; field_update_instance($instance); $this->drupalGet('node/' . $node->nid); $this->assertRaw(t('Cancel rating'), 'User can cancel his vote.'); // Now lets 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 that users can not rate content with exposed widgets that has the 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 an article to test static widget. $node = $this->drupalCreateNode(array('type' => 'article')); // Rate the article. $this->drupalLogin($this->voter); $this->drupalGet('node/' . $node->nid); $this->assertRaw(t('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 article content type. $this->createFivestarField(array('widget_type' => 'exposed')); // Add an article to rate. $node = $this->drupalCreateNode(array('type' => 'article')); // Rate the article. $this->drupalLogin($this->voter); $edit = array( 'vote' => '60', ); $commands = $this->drupalPostAJAX('node/' . $node->nid, $edit, "vote", NULL, array(), array(), "fivestar-custom-widget"); $expected = array( 'command' => 'fivestarUpdate', // TODO: we should text the data being returned. ); $this->assertCommand($commands, $expected, "The fivestarUpdate AJAX command was returned."); } /** * Test that we can switch the fivestar widgets around for the exposed * widget type. */ public function testExposedWidgetDisplay() { // Lets 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', 'article'); // Add an article to test widget against. $node = $this->drupalCreateNode(array('type' => 'article')); // 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', t('The @name fivestar widget is properly display.', array('@name' => $name))); } } /** * Add a fivestar field to a content type. * * @param $options * An associative array of options for the field and instance. */ public function createFivestarField($options = array()) { $options = $options + array( 'content_type' => 'article', 'widget_type' => 'stars', 'display' => array(), ); $field = array( 'field_name' => 'fivestar_test', 'type' => 'fivestar', 'cardinality' => 1, 'settings' => array( 'axis' => 'vote', ), ); $instance = array( 'entity_type' => 'node', 'field_name' => 'fivestar_test', 'label' => 'Fivestar test field', 'bundle' => $options['content_type'], 'widget' => array( 'type' => $options['widget_type'], 'settings' => array( 'widget' => array( 'fivestar_widget' => 'default', ), ), ), 'settings' => array( 'axis' => 'vote', 'stars' => '5', ), 'display' => $options['display'], ); field_create_field($field); field_create_instance($instance); } }