Cleaning up tests.
[project/addthis.git] / addthis.test
1 <?php
2
3 /**
4 * @file
5 * Tests for the AddThis-module.
6 */
7
8 class AddThisFunctionalityTestCase extends DrupalWebTestCase {
9
10 private $user;
11
12 public function setUp() {
13 parent::setUp(AddThis::MODULE_NAME);
14 $this->createAndLoginUser();
15 }
16
17 public function getInfo() {
18 return array(
19 'name' => 'AddThis: Functionality tests',
20 'description' => 'Functionality tests for the AddThis-module.',
21 'group' => 'AddThis',
22 );
23 }
24
25 public function testFunctionality() {
26 $this->addThisShouldProvideHelp();
27
28 $this->addThisShouldDefineField();
29
30 $this->addThisShouldDefineBlock();
31
32 $this->addThisShouldBeAbleToBeUsedAsField();
33
34 $this->addThisSystemSettingsShouldBeAbleToBeSaved();
35 }
36
37 private function addThisShouldProvideHelp() {
38 $helpText = module_invoke(AddThis::MODULE_NAME, 'help', 'admin/help#addthis', NULL);
39 $this->assertTrue(TestHelper::stringContains($helpText, t('About')), 'AddThis-module should provide help.');
40 }
41
42 private function addThisShouldDefineField() {
43 $fieldInfo = module_invoke(AddThis::MODULE_NAME, 'field_info');
44 $this->assertTrue(is_array($fieldInfo), t('AddThis-module should define a field.'));
45 }
46
47 private function addThisShouldDefineBlock() {
48 $blockInfo = module_invoke(AddThis::MODULE_NAME, 'block_info');
49 $this->assertTrue(is_array($blockInfo), t('AddThis-module should define a block.'));
50 }
51
52 private function addThisShouldBeAbleToBeUsedAsField() {
53 $edit = array();
54 $label = TestHelper::generateRandomLowercaseString();
55 $edit["fields[_add_new_field][label]"] = $label;
56 $edit["fields[_add_new_field][field_name]"] = $label;
57 $edit["fields[_add_new_field][type]"] = AddThis::FIELD_TYPE;
58 $edit["fields[_add_new_field][widget_type]"] = AddThis::WIDGET_TYPE;
59
60 $this->drupalPost('admin/structure/types/manage/article/fields', $edit, t('Save'));
61
62 $this->assertText($label, 'AddThis-module should be able to be added as a field.');
63
64 $this->addThisShouldNotHaveConfigurableFieldLevelSettings($label);
65
66 $this->drupalGet('admin/structure/types/manage/article/display');
67 }
68
69 private function addThisShouldNotHaveConfigurableFieldLevelSettings($label) {
70 $this->assertText(t("@name has no field settings.", array('@name' => $label)),
71 'AddThis-module should not have configurable field level settings.');
72 }
73
74 private function addThisSystemSettingsShouldBeAbleToBeSaved() {
75 $edit = array();
76
77 $profileId = TestHelper::generateRandomLowercaseString();
78 $edit[AddThis::PROFILE_ID_KEY] = $profileId;
79
80 $servicesCssUrl = AddThis::DEFAULT_SERVICES_CSS_URL;
81 $edit[AddThis::SERVICES_CSS_URL_KEY] = $servicesCssUrl;
82
83 $servicesJsonUrl = AddThis::DEFAULT_SERVICES_JSON_URL;
84 $edit[AddThis::SERVICES_JSON_URL_KEY] = $servicesJsonUrl;
85
86 $widgetJsUrl = AddThis::DEFAULT_WIDGET_JS_URL;
87 $edit[AddThis::WIDGET_JS_URL_KEY] = $widgetJsUrl;
88
89 $uiDelay = 400;
90 $edit[AddThis::UI_DELAY_KEY] = $uiDelay;
91
92 $uiHeaderBackgroundColor = '#000000';
93 $edit[AddThis::UI_HEADER_BACKGROUND_COLOR_KEY] = $uiHeaderBackgroundColor;
94
95 $uiHeaderColor = '#ffffff';
96 $edit[AddThis::UI_HEADER_COLOR_KEY] = $uiHeaderColor;
97
98 $coBrand = TestHelper::generateRandomLowercaseString();
99 $edit[AddThis::CO_BRAND_KEY] = $coBrand;
100
101 $edit[AddThis::COMPLIANT_508_KEY] = TRUE;
102
103 $edit[AddThis::CLICKBACK_TRACKING_ENABLED_KEY] = TRUE;
104
105 $edit[AddThis::ADDRESSBOOK_ENABLED_KEY] = TRUE;
106
107 $edit[AddThis::CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY] = TRUE;
108
109 $edit[AddThis::OPEN_WINDOWS_ENABLED_KEY] = TRUE;
110
111 $edit[AddThis::STANDARD_CSS_ENABLED_KEY] = TRUE;
112
113 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY] = TRUE;
114
115 $customConfigurationCode = TestHelper::generateRandomLowercaseString();
116 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_KEY] = $customConfigurationCode;
117
118 $serviceName = '2linkme';
119 $edit["addthis_enabled_services[$serviceName]"] = $serviceName;
120
121 $this->drupalPost('admin/config/user-interface/addthis', $edit, t('Save configuration'));
122
123 $this->assertText(t('The configuration options have been saved.'), 'AddThis system settings should be able to be saved.');
124
125 $this->addThisProfileIdShouldBeAbleToBeSaved($profileId);
126
127 $this->addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl);
128
129 $this->addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl);
130
131 $this->addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl);
132
133 $this->addThisUiDelayShouldBeAbleToBeSaved($uiDelay);
134
135 $this->addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor);
136
137 $this->addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor);
138
139 $this->addThisCoBrandShouldBeAbleToBeSaved($coBrand);
140
141 $this->addThis508CompliantSettingShouldBeAbleToBeSaved();
142
143 $this->addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved();
144
145 $this->addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved();
146
147 $this->addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved();
148
149 $this->addThisStandardCssEnabledSettingShouldBeAbleToBeSaved();
150
151 $this->addThisEnabledServicesShouldBeAbleToBeSaved($serviceName);
152
153 $this->addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved();
154 }
155
156 private function addThisProfileIdShouldBeAbleToBeSaved($profileId) {
157 $this->assertFieldByName(AddThis::PROFILE_ID_KEY, $profileId, 'AddThis profile ID should be able to be saved.');
158 }
159
160 private function addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl) {
161 $this->assertFieldByName(AddThis::SERVICES_CSS_URL_KEY, $servicesCssUrl,
162 'AddThis services stylesheet URL should be able to be saved.');
163 }
164
165 private function addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl) {
166 $this->assertFieldByName(AddThis::SERVICES_JSON_URL_KEY, $servicesJsonUrl,
167 'AddThis services json URL should be able to be saved.');
168 }
169
170 private function addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl) {
171 $this->assertFieldByName(AddThis::WIDGET_JS_URL_KEY, $widgetJsUrl,
172 'AddThis javascript widget URL should be able to be saved.');
173 }
174
175 private function addThis508CompliantSettingShouldBeAbleToBeSaved() {
176 $this->assertFieldChecked('edit-addthis-508-compliant',
177 'AddThis 508 compliant setting should be able to be saved.');
178 }
179
180 private function addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved() {
181 $this->assertFieldChecked('edit-addthis-clickback-tracking-enabled',
182 'AddThis clickback tracking enabled setting should be able to be saved.');
183 }
184
185 private function addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved() {
186 $this->assertFieldChecked('edit-addthis-click-to-open-compact-menu-enabled',
187 'AddThis click to open compact menu setting should be able to be saved.');
188 }
189
190 private function addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved() {
191 $this->assertFieldChecked('edit-addthis-open-windows-enabled',
192 'AddThis open windows setting should be able to be saved.');
193 }
194
195 private function addThisStandardCssEnabledSettingShouldBeAbleToBeSaved() {
196 $this->assertFieldChecked('edit-addthis-open-windows-enabled',
197 'AddThis open windows setting should be able to be saved.');
198 }
199
200 private function addThisFacebookLikeEnabledSettingShouldBeAbleToBeSaved() {
201 $this->assertFieldChecked('edit-addthis-facebook-like-enabled',
202 'AddThis Facebook Like enabled setting should be able to be saved.');
203 }
204
205 private function addThisAddressbookEnabledSettingShouldBeAbleToBeSaved() {
206 $this->assertFieldChecked('edit-addthis-addressbook-enabled',
207 'AddThis addressbook enabled setting should be able to be saved.');
208 }
209
210 private function addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved() {
211 $this->assertFieldChecked('edit-addthis-custom-configuration-code-enabled',
212 'AddThis custom configuration code enabled setting should be able to be saved.');
213 }
214
215 private function addThisCustomConfigurationCodeShouldBeAbleToBeSaved($customConfigurationCode) {
216 $this->assertFieldByName(AddThis::CUSTOM_CONFIGURATION_CODE_KEY, $customConfigurationCode,
217 'AddThis custom configuration code should be able to be saved.');
218 }
219
220 private function addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor) {
221 $this->assertFieldByName(AddThis::UI_HEADER_COLOR_KEY, $uiHeaderColor,
222 'AddThis UI header color should be able to be saved.');
223 }
224
225 private function addThisUiDelayShouldBeAbleToBeSaved($uiDelay) {
226 $this->assertFieldByName(AddThis::UI_DELAY_KEY, $uiDelay,
227 'AddThis UI delay should be able to be saved.');
228 }
229
230 private function addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor) {
231 $this->assertFieldByName(AddThis::UI_HEADER_BACKGROUND_COLOR_KEY, $uiHeaderBackgroundColor,
232 'AddThis UI header background color should be able to be saved.');
233 }
234
235 private function addThisCoBrandShouldBeAbleToBeSaved($coBrand) {
236 $this->assertFieldByName(AddThis::CO_BRAND_KEY, $coBrand,
237 'AddThis co-brand should be able to be saved.');
238 }
239
240 private function addThisEnabledServicesShouldBeAbleToBeSaved($serviceName) {
241 $this->assertFieldChecked('edit-addthis-enabled-services-' . $serviceName,
242 'AddThis enabled services should be able to be saved.');
243 }
244
245 private function createAndLoginUser() {
246 $this->user = $this->createAdminUser();
247 $this->drupalLogin($this->user);
248 }
249
250 private function createAdminUser() {
251 return $this->drupalCreateUser(
252 array(
253 AddThis::PERMISSION_ADMINISTER_ADDTHIS,
254 AddThis::PERMISSION_ADMINISTER_ADVANCED_ADDTHIS,
255 'administer blocks',
256 'administer content types',
257 'administer nodes'
258 )
259 );
260 }
261 }
262
263 class AddThisPermissionsTestCase extends DrupalWebTestCase {
264
265 public function setUp() {
266 parent::setUp(AddThis::MODULE_NAME);
267 }
268
269 public function getInfo() {
270 return array(
271 'name' => 'AddThis: Permission tests',
272 'description' => 'Permission tests for the AddThis-module.',
273 'group' => 'AddThis',
274 );
275 }
276
277 public function testUserWithoutAdministerAddThisPermissionShouldNotBeAllowedToAccessAddThisSystemSettings() {
278 $this->drupalLogin($this->createAdminUserWithoutAddThisAdministrationPermission());
279 $this->drupalGet('admin/config/system/addthis');
280 $this->assertRaw(t('Access denied'),
281 'A user without administer addthis permission should not be able to access AddThis system settings.');
282 }
283
284 public function testUserWithoutAdministerAdvancedAddThisPermissionShouldNotBeAllowedToAccessAdvancedAddThisSystemSettings() {
285 $this->drupalLogin($this->createAdminUserWithoutAdvancedAddThisAdministrationPermission());
286 $this->drupalGet('admin/config/system/addthis');
287 $this->assertNoRaw(t('Advanced settings'),
288 'A user without administer advanced addthis permission should not be able to access advanced AddThis system settings.');
289 }
290
291 private function createAdminUserWithoutAddThisAdministrationPermission() {
292 return $this->drupalCreateUser(array('administer content types'));
293 }
294
295 private function createAdminUserWithoutAdvancedAddThisAdministrationPermission() {
296 return $this->drupalCreateUser(array('administer content types', AddThis::PERMISSION_ADMINISTER_ADDTHIS));
297 }
298 }
299
300 class TestHelper {
301
302 public static function stringContains($string, $contains) {
303 return strpos($string, $contains) !== FALSE;
304 }
305
306 public static function generateRandomLowercaseString() {
307 return drupal_strtolower(DrupalWebTestCase::randomName());
308 }
309 }