Added namespace adding for Facebook Like.
[project/addthis.git] / addthis.test
CommitLineData
2422fd60
JP
1<?php
2
3/**
4 * @file
5 * Tests for the AddThis-module.
6 */
7
4da34f42 8class AddThisFunctionalityTestCase extends DrupalWebTestCase {
2422fd60 9
eafc662f
JP
10 private $user;
11
12 public function setUp() {
fad4a20c 13 parent::setUp(AddThis::MODULE_NAME);
4da34f42 14 $this->createAndLoginUser();
eafc662f
JP
15 }
16
2422fd60
JP
17 public function getInfo() {
18 return array(
4da34f42
JP
19 'name' => 'AddThis: Functionality tests',
20 'description' => 'Functionality tests for the AddThis-module.',
2422fd60
JP
21 'group' => 'AddThis',
22 );
23 }
24
05d0530a 25 public function testFunctionality() {
7a62f979 26 $this->addThisShouldProvideHelp();
13d62743 27
7a62f979 28 $this->addThisShouldDefineField();
13d62743 29
7a62f979 30 $this->addThisShouldDefineBlock();
13d62743 31
eafc662f 32 $this->addThisShouldBeAbleToBeUsedAsField();
13d62743 33
eafc662f 34 $this->addThisShouldBeAbleToBeUsedAsBlock();
7a62f979
JP
35
36 $this->addThisSystemSettingsShouldBeAbleToBeSaved();
0d482fd9
JP
37 }
38
7a62f979 39 private function addThisShouldProvideHelp() {
4da34f42
JP
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.');
0d482fd9 42 }
05d0530a 43
7a62f979 44 private function addThisShouldDefineField() {
4da34f42
JP
45 $fieldInfo = module_invoke(AddThis::MODULE_NAME, 'field_info');
46 $this->assertTrue(is_array($fieldInfo), t('AddThis-module should define a field.'));
0d482fd9 47 }
05d0530a 48
7a62f979 49 private function addThisShouldDefineBlock() {
4da34f42
JP
50 $blockInfo = module_invoke(AddThis::MODULE_NAME, 'block_info');
51 $this->assertTrue(is_array($blockInfo), t('AddThis-module should define a block.'));
2422fd60 52 }
05d0530a 53
eafc662f 54 private function addThisShouldBeAbleToBeUsedAsField() {
eafc662f 55 $edit = array();
4da34f42 56 $label = TestHelper::generateRandomLowercaseString();
8a87ba94
JP
57 $edit["fields[_add_new_field][label]"] = $label;
58 $edit["fields[_add_new_field][field_name]"] = $label;
43a336b9
JP
59 $edit["fields[_add_new_field][type]"] = AddThis::FIELD_TYPE;
60 $edit["fields[_add_new_field][widget_type]"] = AddThis::WIDGET_TYPE;
eafc662f
JP
61
62 $this->drupalPost('admin/structure/types/manage/article/fields', $edit, t('Save'));
8a87ba94
JP
63
64 $this->assertText($label, 'AddThis-module should be able to be added as a field.');
13d62743 65
1d9a8337
JP
66 $this->addThisShouldNotHaveConfigurableFieldLevelSettings($label);
67
e0d5a8e5 68 $this->drupalGet('admin/structure/types/manage/article/display');
1d9a8337 69
7a62f979 70 $this->addThisShouldProvideWidgetTypeCompactButton();
13d62743 71
7a62f979 72 $this->addThisShouldProvideWidgetTypeDisabled();
13d62743 73
7a62f979 74 $this->addThisShouldProvideWidgetTypeLargeButton();
13d62743 75
7a62f979 76 $this->addThisShouldProvideWidgetTypeSharecount();
13d62743 77
7a62f979 78 $this->addThisShouldProvideWidgetTypeToolbox();
1d9a8337
JP
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.');
8a87ba94
JP
84 }
85
7a62f979
JP
86 private function addThisShouldProvideWidgetTypeCompactButton() {
87 $this->assertText('Compact button', 'AddThis-module should provide widget type "Compact button"');
8a87ba94
JP
88 }
89
7a62f979
JP
90 private function addThisShouldProvideWidgetTypeDisabled() {
91 $this->assertText('Disabled', 'AddThis-module should provide widget type "Disabled"');
8a87ba94
JP
92 }
93
7a62f979
JP
94 private function addThisShouldProvideWidgetTypeLargeButton() {
95 $this->assertText('Large button', 'AddThis-module should provide widget type "Large button"');
adfbbf32 96 }
8a87ba94 97
7a62f979
JP
98 private function addThisShouldProvideWidgetTypeSharecount() {
99 $this->assertText('Sharecount', 'AddThis-module should provide widget type "Sharecount"');
8a87ba94
JP
100 }
101
7a62f979
JP
102 private function addThisShouldProvideWidgetTypeToolbox() {
103 $this->assertText('Toolbox', 'AddThis-module should provide widget type "Toolbox"');
eafc662f
JP
104 }
105
106 private function addThisShouldBeAbleToBeUsedAsBlock() {
4da34f42 107 $this->drupalGet('admin/structure/block');
a4d290be 108 $this->assertRaw(t('AddThis'), 'AddThis should be able to be used as a block.');
7a62f979
JP
109
110 $this->addThisBlockSettingsShouldBeAbleToBeSaved();
111 }
112
113 private function addThisBlockSettingsShouldBeAbleToBeSaved() {
114 $edit = array();
65e6db6f 115 $edit[AddThis::BLOCK_WIDGET_TYPE_KEY] = AddThis::WIDGET_TYPE_COMPACT_BUTTON;
7a62f979
JP
116
117 $this->drupalPost('admin/structure/block/manage/addthis/addthis_block/configure', $edit, t('Save block'));
118
1d9a8337 119 $this->assertText(t('The block configuration has been saved.'), 'AddThis block settings should be able to be saved.');
7a62f979
JP
120 }
121
122 private function addThisSystemSettingsShouldBeAbleToBeSaved() {
123 $edit = array();
8109364c 124
4da34f42 125 $profileId = TestHelper::generateRandomLowercaseString();
cfab0d33 126 $edit[AddThis::PROFILE_ID_KEY] = $profileId;
8109364c
JP
127
128 $servicesCssUrl = AddThis::DEFAULT_SERVICES_CSS_URL;
129 $edit[AddThis::SERVICES_CSS_URL_KEY] = $servicesCssUrl;
130
00c36dc4
JP
131 $servicesJsonUrl = AddThis::DEFAULT_SERVICES_JSON_URL;
132 $edit[AddThis::SERVICES_JSON_URL_KEY] = $servicesJsonUrl;
8109364c
JP
133
134 $widgetJsUrl = AddThis::DEFAULT_WIDGET_JS_URL;
135 $edit[AddThis::WIDGET_JS_URL_KEY] = $widgetJsUrl;
8abc0f61 136
4aa15ce9
JP
137 $uiDelay = 400;
138 $edit[AddThis::UI_DELAY_KEY] = $uiDelay;
139
57765925
JP
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
e8f5baaa
JP
146 $coBrand = TestHelper::generateRandomLowercaseString();
147 $edit[AddThis::CO_BRAND_KEY] = $coBrand;
148
460f1e3c 149 $edit[AddThis::LARGE_ICONS_ENABLED_KEY] = TRUE;
a3344405 150
90e669c3
JP
151 $edit[AddThis::COMPLIANT_508_KEY] = TRUE;
152
5ce0cb2b 153 $edit[AddThis::CLICKBACK_TRACKING_ENABLED_KEY] = TRUE;
f529b836
MG
154
155 $edit[AddThis::GOOGLE_ANALYTICS_TRACKING_ENABLED_KEY] = TRUE;
156
157 $edit[AddThis::GOOGLE_ANALYTICS_SOCIAL_TRACKING_ENABLED_KEY] = TRUE;
08ff2c73 158
79751477
JP
159 $edit[AddThis::ADDRESSBOOK_ENABLED_KEY] = TRUE;
160
4aa15ce9
JP
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
a3344405
JP
167 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY] = TRUE;
168
169 $customConfigurationCode = TestHelper::generateRandomLowercaseString();
170 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_KEY] = $customConfigurationCode;
a4d290be 171
87bc3b42
JP
172 $serviceName = '2linkme';
173 $edit["addthis_enabled_services[$serviceName]"] = $serviceName;
7a62f979 174
a4d290be 175 $this->drupalPost('admin/config/user-interface/addthis', $edit, t('Save configuration'));
7a62f979 176
87bc3b42
JP
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
8109364c
JP
181 $this->addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl);
182
00c36dc4
JP
183 $this->addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl);
184
8109364c
JP
185 $this->addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl);
186
4aa15ce9
JP
187 $this->addThisUiDelayShouldBeAbleToBeSaved($uiDelay);
188
57765925
JP
189 $this->addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor);
190
191 $this->addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor);
192
e8f5baaa
JP
193 $this->addThisCoBrandShouldBeAbleToBeSaved($coBrand);
194
8abc0f61
JP
195 $this->addThisLargeToolboxIconsSettingShouldBeAbleToBeSaved();
196
90e669c3
JP
197 $this->addThis508CompliantSettingShouldBeAbleToBeSaved();
198
79751477 199 $this->addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved();
08ff2c73 200
f529b836
MG
201 $this->addThisGoogleAnalyticsTrackingEnabledSettingShouldBeAbleToBeSaved();
202
203 $this->addThisGoogleAnalyticsSocialTrackingEnabledSettingShouldBeAbleToBeSaved();
204
4aa15ce9
JP
205 $this->addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved();
206
207 $this->addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved();
208
59bb6deb
JP
209 $this->addThisStandardCssEnabledSettingShouldBeAbleToBeSaved();
210
87bc3b42 211 $this->addThisEnabledServicesShouldBeAbleToBeSaved($serviceName);
a3344405
JP
212
213 $this->addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved();
87bc3b42
JP
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
8109364c 220 private function addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl) {
1d9a8337
JP
221 $this->assertFieldByName(AddThis::SERVICES_CSS_URL_KEY, $servicesCssUrl,
222 'AddThis services stylesheet URL should be able to be saved.');
8109364c
JP
223 }
224
00c36dc4 225 private function addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl) {
1d9a8337
JP
226 $this->assertFieldByName(AddThis::SERVICES_JSON_URL_KEY, $servicesJsonUrl,
227 'AddThis services json URL should be able to be saved.');
00c36dc4
JP
228 }
229
8109364c 230 private function addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl) {
1d9a8337
JP
231 $this->assertFieldByName(AddThis::WIDGET_JS_URL_KEY, $widgetJsUrl,
232 'AddThis javascript widget URL should be able to be saved.');
8109364c
JP
233 }
234
8abc0f61 235 private function addThisLargeToolboxIconsSettingShouldBeAbleToBeSaved() {
1d9a8337
JP
236 $this->assertFieldChecked('edit-addthis-large-icons-enabled',
237 'AddThis large toolbox icons setting should be able to be saved.');
8abc0f61
JP
238 }
239
90e669c3
JP
240 private function addThis508CompliantSettingShouldBeAbleToBeSaved() {
241 $this->assertFieldChecked('edit-addthis-508-compliant',
242 'AddThis 508 compliant setting should be able to be saved.');
243 }
244
79751477 245 private function addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved() {
5ce0cb2b 246 $this->assertFieldChecked('edit-addthis-clickback-tracking-enabled',
79751477 247 'AddThis clickback tracking enabled setting should be able to be saved.');
f529b836
MG
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.');
79751477
JP
258 }
259
4aa15ce9
JP
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
59bb6deb
JP
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
421589c9
JP
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
e1345397
JP
285 private function addThisTwitterEnabledSettingShouldBeAbleToBeSaved() {
286 $this->assertFieldChecked('edit-addthis-twitter-enabled',
287 'AddThis Twitter enabled setting should be able to be saved.');
288 }
289
79751477
JP
290 private function addThisAddressbookEnabledSettingShouldBeAbleToBeSaved() {
291 $this->assertFieldChecked('edit-addthis-addressbook-enabled',
292 'AddThis addressbook enabled setting should be able to be saved.');
08ff2c73
JP
293 }
294
e3447863
JP
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
a3344405 300 private function addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved() {
1d9a8337
JP
301 $this->assertFieldChecked('edit-addthis-custom-configuration-code-enabled',
302 'AddThis custom configuration code enabled setting should be able to be saved.');
a3344405
JP
303 }
304
305 private function addThisCustomConfigurationCodeShouldBeAbleToBeSaved($customConfigurationCode) {
1d9a8337
JP
306 $this->assertFieldByName(AddThis::CUSTOM_CONFIGURATION_CODE_KEY, $customConfigurationCode,
307 'AddThis custom configuration code should be able to be saved.');
a3344405
JP
308 }
309
57765925 310 private function addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor) {
1d9a8337
JP
311 $this->assertFieldByName(AddThis::UI_HEADER_COLOR_KEY, $uiHeaderColor,
312 'AddThis UI header color should be able to be saved.');
4aa15ce9
JP
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.');
57765925
JP
318 }
319
320 private function addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor) {
1d9a8337
JP
321 $this->assertFieldByName(AddThis::UI_HEADER_BACKGROUND_COLOR_KEY, $uiHeaderBackgroundColor,
322 'AddThis UI header background color should be able to be saved.');
57765925
JP
323 }
324
e8f5baaa
JP
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
87bc3b42 330 private function addThisEnabledServicesShouldBeAbleToBeSaved($serviceName) {
1d9a8337
JP
331 $this->assertFieldChecked('edit-addthis-enabled-services-' . $serviceName,
332 'AddThis enabled services should be able to be saved.');
eafc662f
JP
333 }
334
335 private function createAndLoginUser() {
336 $this->user = $this->createAdminUser();
337 $this->drupalLogin($this->user);
338 }
339
340 private function createAdminUser() {
6fa6aaac
JP
341 return $this->drupalCreateUser(
342 array(
460f1e3c
JP
343 AddThis::PERMISSION_ADMINISTER_ADDTHIS,
344 AddThis::PERMISSION_ADMINISTER_ADVANCED_ADDTHIS,
6fa6aaac
JP
345 'administer blocks',
346 'administer content types',
347 'administer nodes'
348 )
349 );
eafc662f 350 }
d3a26af9
JP
351}
352
353class AddThisPermissionsTestCase extends DrupalWebTestCase {
354
d3a26af9
JP
355 public function setUp() {
356 parent::setUp(AddThis::MODULE_NAME);
d3a26af9
JP
357 }
358
359 public function getInfo() {
360 return array(
4da34f42 361 'name' => 'AddThis: Permission tests',
d3a26af9
JP
362 'description' => 'Permission tests for the AddThis-module.',
363 'group' => 'AddThis',
364 );
365 }
366
367 public function testUserWithoutAdministerAddThisPermissionShouldNotBeAllowedToAccessAddThisSystemSettings() {
460f1e3c
JP
368 $this->drupalLogin($this->createAdminUserWithoutAddThisAdministrationPermission());
369 $this->drupalGet('admin/config/system/addthis');
1d9a8337
JP
370 $this->assertRaw(t('Access denied'),
371 'A user without administer addthis permission should not be able to access AddThis system settings.');
460f1e3c
JP
372 }
373
374 public function testUserWithoutAdministerAdvancedAddThisPermissionShouldNotBeAllowedToAccessAdvancedAddThisSystemSettings() {
375 $this->drupalLogin($this->createAdminUserWithoutAdvancedAddThisAdministrationPermission());
4da34f42 376 $this->drupalGet('admin/config/system/addthis');
1d9a8337
JP
377 $this->assertNoRaw(t('Advanced settings'),
378 'A user without administer advanced addthis permission should not be able to access advanced AddThis system settings.');
d3a26af9
JP
379 }
380
381 private function createAdminUserWithoutAddThisAdministrationPermission() {
4da34f42 382 return $this->drupalCreateUser(array('administer content types'));
d3a26af9
JP
383 }
384
460f1e3c
JP
385 private function createAdminUserWithoutAdvancedAddThisAdministrationPermission() {
386 return $this->drupalCreateUser(array('administer content types', AddThis::PERMISSION_ADMINISTER_ADDTHIS));
d3a26af9 387 }
4da34f42
JP
388}
389
390class TestHelper {
d3a26af9 391
4da34f42 392 public static function stringContains($string, $contains) {
d3a26af9
JP
393 return strpos($string, $contains) !== FALSE;
394 }
4da34f42
JP
395
396 public static function generateRandomLowercaseString() {
397 return drupal_strtolower(DrupalWebTestCase::randomName());
398 }
2422fd60 399}