Projet

Général

Profil

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

root / drupal7 / sites / all / modules / fivestar / test / fivestar.field.test @ 3461d8cb

1
<?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
    // Load the instance settings so we can set allow_ownvote.
25
    $instance = field_read_instance('node', 'fivestar_test', 'test_node_type');
26
    $instance['settings']['allow_ownvote'] = 1;
27
    field_update_instance($instance);
28
    // Save an test_node_typee node with a two-star rating.
29
    $edit = array(
30
      'title' => $this->randomString(),
31
      'fivestar_test[und][0][rating]' => '40' // Equals a rating of 2 stars.
32
    );
33
    $this->drupalPost('node/add/test_node_type', $edit, t('Save'));
34
    // Make sure the two-star rating shows on the node view.
35
    $result = $this->xpath("//div[contains(@class, 'field-name-fivestar-test')]//div[contains(@class,'star-first')]/span");
36
    $this->assertEqual($result[0][0], '2', 'Content authors can rate their own content using the stars widget.');
37
  }
38

    
39
  /**
40
   * Test that users can rate content with exposed widgets.
41
   */
42
  public function testViewerRating() {
43
    $this->createFivestarField(array('widget_type' => 'exposed'));
44
    // Add an test_node_type to rate.
45
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
46
    $this->drupalGet('node/' . $node->nid);
47
    $this->drupalLogin($this->voter_user);
48

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

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

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

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

    
75
    // Now lets change the field to have exposed off and make sure the value is still there.
76
    // @see http://drupal.org/node/1242082
77
    $instance['display']['default']['settings']['expose'] = FALSE;
78
    field_update_instance($instance);
79
    $this->drupalGet('node/' . $node->nid);
80
    $this->assertFalse($this->xpath("//form[contains(@class, 'fivestar-widget')]"));
81
    // Make sure the three-star rating still shows on the node view.
82
    $result = $this->xpath("//div[contains(@class, 'field-name-fivestar-test')]//div[contains(@class,'star-first')]/span");
83
    $this->assertEqual($result[0][0], '3', 'The static display still shows three stars.');
84
  }
85

    
86
  /**
87
   * Test that users can not rate content with exposed widgets that has the exposed
88
   * display setting set to FALSE.
89
   */
90
  public function testViewerNonRating() {
91
    // Add an exposed field, with the Exposed display settings set to FALSE.
92
    $this->createFivestarField(array(
93
      'widget_type' => 'exposed',
94
      'display' => array(
95
        'default' => array(
96
          'type' => 'fivestar_formatter_default',
97
          'settings' => array(
98
            'style' => 'average',
99
            'text' => 'average',
100
            'expose' => FALSE,
101
          ),
102
        ),
103
      ),
104
    ));
105
    // Add an test_node_type to test static widget.
106
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
107
    // Rate the test_node_type.
108
    $this->drupalLogin($this->voter_user);
109
    $this->drupalGet('node/' . $node->nid);
110
    $this->assertRaw(t('No votes yet'), 'Fivestar field has no votes.');
111
    $this->assertFalse($this->xpath("//form[contains(@class, 'fivestar-widget')]"));
112
  }
113

    
114
  /**
115
   * Test that users can rate content with exposed widgets.
116
   */
117
  public function testViewerRatingAjax() {
118
    // Add a viewer-rated fivestar field to the test_node_type content type.
119
    $this->createFivestarField(array('widget_type' => 'exposed'));
120
    // Add an test_node_type to rate.
121
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
122
    // Rate the test_node_type.
123
    $this->drupalLogin($this->voter_user);
124
    $edit = array(
125
      'vote' => '60',
126
    );
127
    $commands = $this->drupalPostAJAX('node/' . $node->nid, $edit, "vote",
128
      NULL, array(), array(), "fivestar-custom-widget");
129
    $expected = array(
130
      'command' => 'fivestarUpdate',
131
      // TODO: we should text the data being returned.
132
    );
133
    $this->assertCommand($commands, $expected, "The fivestarUpdate AJAX command was returned.");
134
  }
135

    
136
  /**
137
   * Test that we can switch the fivestar widgets around for the exposed
138
   * widget type.
139
   */
140
  public function testExposedWidgetDisplay() {
141
    // Lets add an exposed widget but display the static widget.
142
    // It's simpler to compare the display type using the static widget.
143
    $this->createFivestarField(array(
144
      'widget_type' => 'exposed',
145
      'display' => array(
146
        'default' => array(
147
          'type' => 'fivestar_formatter_default',
148
          'settings' => array(
149
            'style' => 'average',
150
            'text' => 'average',
151
            'expose' => FALSE,
152
          ),
153
        ),
154
      ),
155
    ));
156
    $instance = field_read_instance('node', 'fivestar_test', 'test_node_type');
157
    // Add an test_node_type to test widget against.
158
    $node = $this->drupalCreateNode(array('type' => 'test_node_type'));
159

    
160
    // Test the Default Widget.
161
    $this->drupalGet('node/' . $node->nid);
162
    $this->assertTrue($this->xpath("//div[contains(@class, 'fivestar-default')]//div[contains(@class,'star-first')]/span"));
163

    
164
    $widgets = module_invoke_all('fivestar_widgets');
165
    foreach ($widgets as $path => $name) {
166
      $instance['display']['default']['settings']['widget']['fivestar_widget'] = $path;
167
      field_update_instance($instance);
168
      $widget_class = 'fivestar-' . drupal_strtolower($name);
169
      $this->drupalGet('node/' . $node->nid);
170
      $result = $this->xpath("//div[contains(@class, '" . $widget_class . "')]//div[contains(@class,'star-first')]/span");
171
      $this->assertEqual($result[0][0], '0', t('The @name fivestar widget is properly display.', array('@name' => $name)));
172
    }
173
  }
174
}