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