diff --git a/fivestar.info b/fivestar.info index 6e15de8..a3c6760 100644 --- a/fivestar.info +++ b/fivestar.info @@ -4,3 +4,4 @@ package = Voting core = 7.x dependencies[] = votingapi configure = admin/config/content/fivestar +files[] = fivestar.test diff --git a/fivestar.test b/fivestar.test new file mode 100644 index 0000000..0e303ae --- /dev/null +++ b/fivestar.test @@ -0,0 +1,96 @@ + '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')); + // TODO: The exposed widget's no-JS fallback is currently broken. + // $this->assertNoRaw(t('No votes yet'), 'Visitors can rate content using the exposed widget.'); + } + + /** + * 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', + ); + $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( + 'axis' => 'vote', + 'stars' => '5', + ), + ); + $field = field_create_field($field); + $instance = field_create_instance($instance); + } +}