Added namespace adding for Facebook Like.
[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->addThisShouldBeAbleToBeUsedAsBlock();
35
36 $this->addThisSystemSettingsShouldBeAbleToBeSaved();
37 }
38
39 private function addThisShouldProvideHelp() {
40 $helpText = module_invoke(AddThis::MODULE_NAME, 'help', 'admin/help#addthis', NULL);
41 $this->assertTrue(TestHelper::stringContains($helpText, t('About')), 'AddThis-module should provide help.');
42 }
43
44 private function addThisShouldDefineField() {
45 $fieldInfo = module_invoke(AddThis::MODULE_NAME, 'field_info');
46 $this->assertTrue(is_array($fieldInfo), t('AddThis-module should define a field.'));
47 }
48
49 private function addThisShouldDefineBlock() {
50 $blockInfo = module_invoke(AddThis::MODULE_NAME, 'block_info');
51 $this->assertTrue(is_array($blockInfo), t('AddThis-module should define a block.'));
52 }
53
54 private function addThisShouldBeAbleToBeUsedAsField() {
55 $edit = array();
56 $label = TestHelper::generateRandomLowercaseString();
57 $edit["fields[_add_new_field][label]"] = $label;
58 $edit["fields[_add_new_field][field_name]"] = $label;
59 $edit["fields[_add_new_field][type]"] = AddThis::FIELD_TYPE;
60 $edit["fields[_add_new_field][widget_type]"] = AddThis::WIDGET_TYPE;
61
62 $this->drupalPost('admin/structure/types/manage/article/fields', $edit, t('Save'));
63
64 $this->assertText($label, 'AddThis-module should be able to be added as a field.');
65
66 $this->addThisShouldNotHaveConfigurableFieldLevelSettings($label);
67
68 $this->drupalGet('admin/structure/types/manage/article/display');
69
70 $this->addThisShouldProvideWidgetTypeCompactButton();
71
72 $this->addThisShouldProvideWidgetTypeDisabled();
73
74 $this->addThisShouldProvideWidgetTypeLargeButton();
75
76 $this->addThisShouldProvideWidgetTypeSharecount();
77
78 $this->addThisShouldProvideWidgetTypeToolbox();
79 }
80
81 private function addThisShouldNotHaveConfigurableFieldLevelSettings($label) {
82 $this->assertText(t("@name has no field settings.", array('@name' => $label)),
83 'AddThis-module should not have configurable field level settings.');
84 }
85
86 private function addThisShouldProvideWidgetTypeCompactButton() {
87 $this->assertText('Compact button', 'AddThis-module should provide widget type "Compact button"');
88 }
89
90 private function addThisShouldProvideWidgetTypeDisabled() {
91 $this->assertText('Disabled', 'AddThis-module should provide widget type "Disabled"');
92 }
93
94 private function addThisShouldProvideWidgetTypeLargeButton() {
95 $this->assertText('Large button', 'AddThis-module should provide widget type "Large button"');
96 }
97
98 private function addThisShouldProvideWidgetTypeSharecount() {
99 $this->assertText('Sharecount', 'AddThis-module should provide widget type "Sharecount"');
100 }
101
102 private function addThisShouldProvideWidgetTypeToolbox() {
103 $this->assertText('Toolbox', 'AddThis-module should provide widget type "Toolbox"');
104 }
105
106 private function addThisShouldBeAbleToBeUsedAsBlock() {
107 $this->drupalGet('admin/structure/block');
108 $this->assertRaw(t('AddThis'), 'AddThis should be able to be used as a block.');
109
110 $this->addThisBlockSettingsShouldBeAbleToBeSaved();
111 }
112
113 private function addThisBlockSettingsShouldBeAbleToBeSaved() {
114 $edit = array();
115 $edit[AddThis::BLOCK_WIDGET_TYPE_KEY] = AddThis::WIDGET_TYPE_COMPACT_BUTTON;
116
117 $this->drupalPost('admin/structure/block/manage/addthis/addthis_block/configure', $edit, t('Save block'));
118
119 $this->assertText(t('The block configuration has been saved.'), 'AddThis block settings should be able to be saved.');
120 }
121
122 private function addThisSystemSettingsShouldBeAbleToBeSaved() {
123 $edit = array();
124
125 $profileId = TestHelper::generateRandomLowercaseString();
126 $edit[AddThis::PROFILE_ID_KEY] = $profileId;
127
128 $servicesCssUrl = AddThis::DEFAULT_SERVICES_CSS_URL;
129 $edit[AddThis::SERVICES_CSS_URL_KEY] = $servicesCssUrl;
130
131 $servicesJsonUrl = AddThis::DEFAULT_SERVICES_JSON_URL;
132 $edit[AddThis::SERVICES_JSON_URL_KEY] = $servicesJsonUrl;
133
134 $widgetJsUrl = AddThis::DEFAULT_WIDGET_JS_URL;
135 $edit[AddThis::WIDGET_JS_URL_KEY] = $widgetJsUrl;
136
137 $uiDelay = 400;
138 $edit[AddThis::UI_DELAY_KEY] = $uiDelay;
139
140 $uiHeaderBackgroundColor = '#000000';
141 $edit[AddThis::UI_HEADER_BACKGROUND_COLOR_KEY] = $uiHeaderBackgroundColor;
142
143 $uiHeaderColor = '#ffffff';
144 $edit[AddThis::UI_HEADER_COLOR_KEY] = $uiHeaderColor;
145
146 $coBrand = TestHelper::generateRandomLowercaseString();
147 $edit[AddThis::CO_BRAND_KEY] = $coBrand;
148
149 $edit[AddThis::LARGE_ICONS_ENABLED_KEY] = TRUE;
150
151 $edit[AddThis::COMPLIANT_508_KEY] = TRUE;
152
153 $edit[AddThis::CLICKBACK_TRACKING_ENABLED_KEY] = TRUE;
154
155 $edit[AddThis::GOOGLE_ANALYTICS_TRACKING_ENABLED_KEY] = TRUE;
156
157 $edit[AddThis::GOOGLE_ANALYTICS_SOCIAL_TRACKING_ENABLED_KEY] = TRUE;
158
159 $edit[AddThis::ADDRESSBOOK_ENABLED_KEY] = TRUE;
160
161 $edit[AddThis::CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY] = TRUE;
162
163 $edit[AddThis::OPEN_WINDOWS_ENABLED_KEY] = TRUE;
164
165 $edit[AddThis::STANDARD_CSS_ENABLED_KEY] = TRUE;
166
167 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY] = TRUE;
168
169 $customConfigurationCode = TestHelper::generateRandomLowercaseString();
170 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_KEY] = $customConfigurationCode;
171
172 $serviceName = '2linkme';
173 $edit["addthis_enabled_services[$serviceName]"] = $serviceName;
174
175 $this->drupalPost('admin/config/user-interface/addthis', $edit, t('Save configuration'));
176
177 $this->assertText(t('The configuration options have been saved.'), 'AddThis system settings should be able to be saved.');
178
179 $this->addThisProfileIdShouldBeAbleToBeSaved($profileId);
180
181 $this->addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl);
182
183 $this->addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl);
184
185 $this->addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl);
186
187 $this->addThisUiDelayShouldBeAbleToBeSaved($uiDelay);
188
189 $this->addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor);
190
191 $this->addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor);
192
193 $this->addThisCoBrandShouldBeAbleToBeSaved($coBrand);
194
195 $this->addThisLargeToolboxIconsSettingShouldBeAbleToBeSaved();
196
197 $this->addThis508CompliantSettingShouldBeAbleToBeSaved();
198
199 $this->addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved();
200
201 $this->addThisGoogleAnalyticsTrackingEnabledSettingShouldBeAbleToBeSaved();
202
203 $this->addThisGoogleAnalyticsSocialTrackingEnabledSettingShouldBeAbleToBeSaved();
204
205 $this->addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved();
206
207 $this->addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved();
208
209 $this->addThisStandardCssEnabledSettingShouldBeAbleToBeSaved();
210
211 $this->addThisEnabledServicesShouldBeAbleToBeSaved($serviceName);
212
213 $this->addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved();
214 }
215
216 private function addThisProfileIdShouldBeAbleToBeSaved($profileId) {
217 $this->assertFieldByName(AddThis::PROFILE_ID_KEY, $profileId, 'AddThis profile ID should be able to be saved.');
218 }
219
220 private function addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl) {
221 $this->assertFieldByName(AddThis::SERVICES_CSS_URL_KEY, $servicesCssUrl,
222 'AddThis services stylesheet URL should be able to be saved.');
223 }
224
225 private function addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl) {
226 $this->assertFieldByName(AddThis::SERVICES_JSON_URL_KEY, $servicesJsonUrl,
227 'AddThis services json URL should be able to be saved.');
228 }
229
230 private function addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl) {
231 $this->assertFieldByName(AddThis::WIDGET_JS_URL_KEY, $widgetJsUrl,
232 'AddThis javascript widget URL should be able to be saved.');
233 }
234
235 private function addThisLargeToolboxIconsSettingShouldBeAbleToBeSaved() {
236 $this->assertFieldChecked('edit-addthis-large-icons-enabled',
237 'AddThis large toolbox icons setting should be able to be saved.');
238 }
239
240 private function addThis508CompliantSettingShouldBeAbleToBeSaved() {
241 $this->assertFieldChecked('edit-addthis-508-compliant',
242 'AddThis 508 compliant setting should be able to be saved.');
243 }
244
245 private function addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved() {
246 $this->assertFieldChecked('edit-addthis-clickback-tracking-enabled',
247 'AddThis clickback tracking enabled setting should be able to be saved.');
248 }
249
250 private function addThisGoogleAnalyticsTrackingEnabledSettingShouldBeAbleToBeSaved() {
251 $this->assertFieldChecked('edit-addthis-google-analytics-tracking-enabled',
252 'AddThis Google Analytics tracking enabled setting should be able to be saved.');
253 }
254
255 private function addThisGoogleAnalyticsSocialTrackingEnabledSettingShouldBeAbleToBeSaved() {
256 $this->assertFieldChecked('edit-addthis-google-analytics-tracking-enabled',
257 'AddThis Google Analytics social tracking enabled setting should be able to be saved.');
258 }
259
260 private function addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved() {
261 $this->assertFieldChecked('edit-addthis-click-to-open-compact-menu-enabled',
262 'AddThis click to open compact menu setting should be able to be saved.');
263 }
264
265 private function addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved() {
266 $this->assertFieldChecked('edit-addthis-open-windows-enabled',
267 'AddThis open windows setting should be able to be saved.');
268 }
269
270 private function addThisStandardCssEnabledSettingShouldBeAbleToBeSaved() {
271 $this->assertFieldChecked('edit-addthis-open-windows-enabled',
272 'AddThis open windows setting should be able to be saved.');
273 }
274
275 private function addThisFacebookLikeEnabledSettingShouldBeAbleToBeSaved() {
276 $this->assertFieldChecked('edit-addthis-facebook-like-enabled',
277 'AddThis Facebook Like enabled setting should be able to be saved.');
278 }
279
280 private function addThisGooglePlusOneEnabledSettingShouldBeAbleToBeSaved() {
281 $this->assertFieldChecked('edit-addthis-google-plus-one-enabled',
282 'AddThis Google +1 enabled setting should be able to be saved.');
283 }
284
285 private function addThisTwitterEnabledSettingShouldBeAbleToBeSaved() {
286 $this->assertFieldChecked('edit-addthis-twitter-enabled',
287 'AddThis Twitter enabled setting should be able to be saved.');
288 }
289
290 private function addThisAddressbookEnabledSettingShouldBeAbleToBeSaved() {
291 $this->assertFieldChecked('edit-addthis-addressbook-enabled',
292 'AddThis addressbook enabled setting should be able to be saved.');
293 }
294
295 private function addThisNumberOfPreferredServicesShouldBeAbleToBeSaved($numberOfPreferredServices) {
296 $this->assertFieldByName(AddThis::NUMBER_OF_PREFERRED_SERVICES_KEY, $numberOfPreferredServices,
297 'The number of preferred services should be able to be saved.');
298 }
299
300 private function addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved() {
301 $this->assertFieldChecked('edit-addthis-custom-configuration-code-enabled',
302 'AddThis custom configuration code enabled setting should be able to be saved.');
303 }
304
305 private function addThisCustomConfigurationCodeShouldBeAbleToBeSaved($customConfigurationCode) {
306 $this->assertFieldByName(AddThis::CUSTOM_CONFIGURATION_CODE_KEY, $customConfigurationCode,
307 'AddThis custom configuration code should be able to be saved.');
308 }
309
310 private function addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor) {
311 $this->assertFieldByName(AddThis::UI_HEADER_COLOR_KEY, $uiHeaderColor,
312 'AddThis UI header color should be able to be saved.');
313 }
314
315 private function addThisUiDelayShouldBeAbleToBeSaved($uiDelay) {
316 $this->assertFieldByName(AddThis::UI_DELAY_KEY, $uiDelay,
317 'AddThis UI delay should be able to be saved.');
318 }
319
320 private function addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor) {
321 $this->assertFieldByName(AddThis::UI_HEADER_BACKGROUND_COLOR_KEY, $uiHeaderBackgroundColor,
322 'AddThis UI header background color should be able to be saved.');
323 }
324
325 private function addThisCoBrandShouldBeAbleToBeSaved($coBrand) {
326 $this->assertFieldByName(AddThis::CO_BRAND_KEY, $coBrand,
327 'AddThis co-brand should be able to be saved.');
328 }
329
330 private function addThisEnabledServicesShouldBeAbleToBeSaved($serviceName) {
331 $this->assertFieldChecked('edit-addthis-enabled-services-' . $serviceName,
332 'AddThis enabled services should be able to be saved.');
333 }
334
335 private function createAndLoginUser() {
336 $this->user = $this->createAdminUser();
337 $this->drupalLogin($this->user);
338 }
339
340 private function createAdminUser() {
341 return $this->drupalCreateUser(
342 array(
343 AddThis::PERMISSION_ADMINISTER_ADDTHIS,
344 AddThis::PERMISSION_ADMINISTER_ADVANCED_ADDTHIS,
345 'administer blocks',
346 'administer content types',
347 'administer nodes'
348 )
349 );
350 }
351 }
352
353 class AddThisPermissionsTestCase extends DrupalWebTestCase {
354
355 public function setUp() {
356 parent::setUp(AddThis::MODULE_NAME);
357 }
358
359 public function getInfo() {
360 return array(
361 'name' => 'AddThis: Permission tests',
362 'description' => 'Permission tests for the AddThis-module.',
363 'group' => 'AddThis',
364 );
365 }
366
367 public function testUserWithoutAdministerAddThisPermissionShouldNotBeAllowedToAccessAddThisSystemSettings() {
368 $this->drupalLogin($this->createAdminUserWithoutAddThisAdministrationPermission());
369 $this->drupalGet('admin/config/system/addthis');
370 $this->assertRaw(t('Access denied'),
371 'A user without administer addthis permission should not be able to access AddThis system settings.');
372 }
373
374 public function testUserWithoutAdministerAdvancedAddThisPermissionShouldNotBeAllowedToAccessAdvancedAddThisSystemSettings() {
375 $this->drupalLogin($this->createAdminUserWithoutAdvancedAddThisAdministrationPermission());
376 $this->drupalGet('admin/config/system/addthis');
377 $this->assertNoRaw(t('Advanced settings'),
378 'A user without administer advanced addthis permission should not be able to access advanced AddThis system settings.');
379 }
380
381 private function createAdminUserWithoutAddThisAdministrationPermission() {
382 return $this->drupalCreateUser(array('administer content types'));
383 }
384
385 private function createAdminUserWithoutAdvancedAddThisAdministrationPermission() {
386 return $this->drupalCreateUser(array('administer content types', AddThis::PERMISSION_ADMINISTER_ADDTHIS));
387 }
388 }
389
390 class TestHelper {
391
392 public static function stringContains($string, $contains) {
393 return strpos($string, $contains) !== FALSE;
394 }
395
396 public static function generateRandomLowercaseString() {
397 return drupal_strtolower(DrupalWebTestCase::randomName());
398 }
399 }