Projet

Général

Profil

Paste
Télécharger (6,35 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / fivestar / test / fivestar.field.test @ a5572547

1 85ad3d82 Assos Assos
<?php
2
/**
3
 * @file
4
 * Simpletests for the Fivestar module.
5
 */
6
7
class FivestarTestCase extends FivestarBaseTestCase {
8
9
  public static function getInfo() {
10
    return array(
11
      'name' => 'Fivestar widgets',
12
      'description' => 'Make sure fivestar widgets can be created and used.',
13
      'group' => 'Fivestar',
14
    );
15
  }
16
17
  /**
18
   * Test that authors can rate their own content.
19
   */
20
  public function testAuthorRating() {
21
    $this->drupalLogin($this->admin_user);
22
    // Add an author-rated fivestar field to the test_node_type content type.
23
    $this->createFivestarField(array('widget_type' => 'stars'));
24
    // Save an test_node_typee node with a two-star rating.
25
    $edit = array(
26
      'title' => $this->randomString(),
27
      'fivestar_test[und][0][rating]' => '40' // Equals a rating of 2 stars.
28
    );
29
    $this->drupalPost('node/add/test_node_type', $edit, t('Save'));
30
    // Make sure the two-star rating shows on the node view.
31
    $result = $this->xpath("//div[contains(@class, 'field-name-fivestar-test')]//div[contains(@class,'star-first')]/span");
32
    $this->assertEqual($result[0][0], '2', 'Content authors can rate their own content using the stars widget.');
33
  }
34
35
  /**
36
   * Test that users can rate content with exposed widgets.
37
   */
38
  public function testViewerRating() {
39
    $this->createFivestarField(array('widget_type' => 'exposed'));
40
    // Add an test_node_type to rate.
41
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
42
    $this->drupalGet('node/' . $node->nid);
43
    $this->drupalLogin($this->voter_user);
44
45
    // Rate the test_node_type.
46
    $edit = array(
47
      'vote' => '60',
48
    );
49
    $this->drupalPost('node/' . $node->nid, $edit, t('Rate'));
50
    $this->assertNoRaw(t('No votes yet'), 'Visitors can rate content using the exposed widget.');
51
52
    // Load the instance settings so we can change certain settings.
53
    $instance = field_read_instance('node', 'fivestar_test', 'test_node_type');
54
55
    // Lets test to make sure the cancel option is not available if disabled.
56
    // @see http://drupal.org/node/1269276
57
    $this->assertNoRaw(t('Cancel rating'), 'User cannot cancel his vote.');
58
    $instance['settings']['allow_clear'] = 1;
59
    field_update_instance($instance);
60
    $this->drupalGet('node/' . $node->nid);
61
    $this->assertRaw(t('Cancel rating'), 'User can cancel his vote.');
62
63
    // Now lets change the field to have exposed off and make sure the value is still there.
64
    // @see http://drupal.org/node/1242082
65
    $instance['display']['default']['settings']['expose'] = FALSE;
66
    field_update_instance($instance);
67
    $this->drupalGet('node/' . $node->nid);
68
    $this->assertFalse($this->xpath("//form[contains(@class, 'fivestar-widget')]"));
69
    // Make sure the three-star rating still shows on the node view.
70
    $result = $this->xpath("//div[contains(@class, 'field-name-fivestar-test')]//div[contains(@class,'star-first')]/span");
71
    $this->assertEqual($result[0][0], '3', 'The static display still shows three stars.');
72
  }
73
74
  /**
75
   * Test that users can not rate content with exposed widgets that has the exposed
76
   * display setting set to FALSE.
77
   */
78
  public function testViewerNonRating() {
79
    // Add an exposed field, with the Exposed display settings set to FALSE.
80
    $this->createFivestarField(array(
81
      'widget_type' => 'exposed',
82
      'display' => array(
83
        'default' => array(
84
          'type' => 'fivestar_formatter_default',
85
          'settings' => array(
86
            'style' => 'average',
87
            'text' => 'average',
88
            'expose' => FALSE,
89
          ),
90
        ),
91
      ),
92
    ));
93
    // Add an test_node_type to test static widget.
94
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
95
    // Rate the test_node_type.
96
    $this->drupalLogin($this->voter_user);
97
    $this->drupalGet('node/' . $node->nid);
98
    $this->assertRaw(t('No votes yet'), 'Fivestar field has no votes.');
99
    $this->assertFalse($this->xpath("//form[contains(@class, 'fivestar-widget')]"));
100
  }
101
102
  /**
103
   * Test that users can rate content with exposed widgets.
104
   */
105
  public function testViewerRatingAjax() {
106
    // Add a viewer-rated fivestar field to the test_node_type content type.
107
    $this->createFivestarField(array('widget_type' => 'exposed'));
108
    // Add an test_node_type to rate.
109
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
110
    // Rate the test_node_type.
111
    $this->drupalLogin($this->voter_user);
112
    $edit = array(
113
      'vote' => '60',
114
    );
115
    $commands = $this->drupalPostAJAX('node/' . $node->nid, $edit, "vote",
116
      NULL, array(), array(), "fivestar-custom-widget");
117
    $expected = array(
118
      'command' => 'fivestarUpdate',
119
      // TODO: we should text the data being returned.
120
    );
121
    $this->assertCommand($commands, $expected, "The fivestarUpdate AJAX command was returned.");
122
  }
123
124
  /**
125
   * Test that we can switch the fivestar widgets around for the exposed
126
   * widget type.
127
   */
128
  public function testExposedWidgetDisplay() {
129
    // Lets add an exposed widget but display the static widget.
130
    // It's simpler to compare the display type using the static widget.
131
    $this->createFivestarField(array(
132
      'widget_type' => 'exposed',
133
      'display' => array(
134
        'default' => array(
135
          'type' => 'fivestar_formatter_default',
136
          'settings' => array(
137
            'style' => 'average',
138
            'text' => 'average',
139
            'expose' => FALSE,
140
          ),
141
        ),
142
      ),
143
    ));
144
    $instance = field_read_instance('node', 'fivestar_test', 'test_node_type');
145
    // Add an test_node_type to test widget against.
146
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
147
148
    // Test the Default Widget.
149
    $this->drupalGet('node/' . $node->nid);
150
    $this->assertTrue($this->xpath("//div[contains(@class, 'fivestar-default')]//div[contains(@class,'star-first')]/span"));
151
152
    $widgets = module_invoke_all('fivestar_widgets');
153
    foreach ($widgets as $path => $name) {
154
      $instance['display']['default']['settings']['widget']['fivestar_widget'] = $path;
155
      field_update_instance($instance);
156
      $widget_class = 'fivestar-' . drupal_strtolower($name);
157
      $this->drupalGet('node/' . $node->nid);
158
      $result = $this->xpath("//div[contains(@class, '" . $widget_class . "')]//div[contains(@class,'star-first')]/span");
159
      $this->assertEqual($result[0][0], '0', t('The @name fivestar widget is properly display.', array('@name' => $name)));
160
    }
161
  }
162
}