Issue #1313732 by matglas86: Added Twitter via support for twitter button.
[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::GOOGLE_ANALYTICS_TRACKING_ENABLED_KEY] = TRUE;
106
107 $edit[AddThis::GOOGLE_ANALYTICS_SOCIAL_TRACKING_ENABLED_KEY] = TRUE;
108
109 $edit[AddThis::ADDRESSBOOK_ENABLED_KEY] = TRUE;
110
111 $edit[AddThis::CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY] = TRUE;
112
113 $edit[AddThis::OPEN_WINDOWS_ENABLED_KEY] = TRUE;
114
115 $edit[AddThis::STANDARD_CSS_ENABLED_KEY] = TRUE;
116
117 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY] = TRUE;
118
119 $customConfigurationCode = TestHelper::generateRandomLowercaseString();
120 $edit[AddThis::CUSTOM_CONFIGURATION_CODE_KEY] = $customConfigurationCode;
121
122 $serviceName = '2linkme';
123 $edit["addthis_enabled_services[$serviceName]"] = $serviceName;
124
125 $this->drupalPost('admin/config/user-interface/addthis', $edit, t('Save configuration'));
126
127 $this->assertText(t('The configuration options have been saved.'), 'AddThis system settings should be able to be saved.');
128
129 $this->addThisProfileIdShouldBeAbleToBeSaved($profileId);
130
131 $this->addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl);
132
133 $this->addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl);
134
135 $this->addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl);
136
137 $this->addThisUiDelayShouldBeAbleToBeSaved($uiDelay);
138
139 $this->addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor);
140
141 $this->addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor);
142
143 $this->addThisCoBrandShouldBeAbleToBeSaved($coBrand);
144
145 $this->addThis508CompliantSettingShouldBeAbleToBeSaved();
146
147 $this->addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved();
148
149 $this->addThisGoogleAnalyticsTrackingEnabledSettingShouldBeAbleToBeSaved();
150
151 $this->addThisGoogleAnalyticsSocialTrackingEnabledSettingShouldBeAbleToBeSaved();
152
153 $this->addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved();
154
155 $this->addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved();
156
157 $this->addThisStandardCssEnabledSettingShouldBeAbleToBeSaved();
158
159 $this->addThisEnabledServicesShouldBeAbleToBeSaved($serviceName);
160
161 $this->addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved();
162 }
163
164 private function addThisProfileIdShouldBeAbleToBeSaved($profileId) {
165 $this->assertFieldByName(AddThis::PROFILE_ID_KEY, $profileId, 'AddThis profile ID should be able to be saved.');
166 }
167
168 private function addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl) {
169 $this->assertFieldByName(AddThis::SERVICES_CSS_URL_KEY, $servicesCssUrl,
170 'AddThis services stylesheet URL should be able to be saved.');
171 }
172
173 private function addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl) {
174 $this->assertFieldByName(AddThis::SERVICES_JSON_URL_KEY, $servicesJsonUrl,
175 'AddThis services json URL should be able to be saved.');
176 }
177
178 private function addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl) {
179 $this->assertFieldByName(AddThis::WIDGET_JS_URL_KEY, $widgetJsUrl,
180 'AddThis javascript widget URL should be able to be saved.');
181 }
182
183 private function addThis508CompliantSettingShouldBeAbleToBeSaved() {
184 $this->assertFieldChecked('edit-addthis-508-compliant',
185 'AddThis 508 compliant setting should be able to be saved.');
186 }
187
188 private function addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved() {
189 $this->assertFieldChecked('edit-addthis-clickback-tracking-enabled',
190 'AddThis clickback tracking enabled setting should be able to be saved.');
191 }
192
193 private function addThisGoogleAnalyticsTrackingEnabledSettingShouldBeAbleToBeSaved() {
194 $this->assertFieldChecked('edit-addthis-google-analytics-tracking-enabled',
195 'AddThis Google Analytics tracking enabled setting should be able to be saved.');
196 }
197
198 private function addThisGoogleAnalyticsSocialTrackingEnabledSettingShouldBeAbleToBeSaved() {
199 $this->assertFieldChecked('edit-addthis-google-analytics-tracking-enabled',
200 'AddThis Google Analytics social tracking enabled setting should be able to be saved.');
201 }
202
203 private function addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved() {
204 $this->assertFieldChecked('edit-addthis-click-to-open-compact-menu-enabled',
205 'AddThis click to open compact menu setting should be able to be saved.');
206 }
207
208 private function addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved() {
209 $this->assertFieldChecked('edit-addthis-open-windows-enabled',
210 'AddThis open windows setting should be able to be saved.');
211 }
212
213 private function addThisStandardCssEnabledSettingShouldBeAbleToBeSaved() {
214 $this->assertFieldChecked('edit-addthis-open-windows-enabled',
215 'AddThis open windows setting should be able to be saved.');
216 }
217
218 private function addThisFacebookLikeEnabledSettingShouldBeAbleToBeSaved() {
219 $this->assertFieldChecked('edit-addthis-facebook-like-enabled',
220 'AddThis Facebook Like enabled setting should be able to be saved.');
221 }
222
223 private function addThisAddressbookEnabledSettingShouldBeAbleToBeSaved() {
224 $this->assertFieldChecked('edit-addthis-addressbook-enabled',
225 'AddThis addressbook enabled setting should be able to be saved.');
226 }
227
228 private function addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved() {
229 $this->assertFieldChecked('edit-addthis-custom-configuration-code-enabled',
230 'AddThis custom configuration code enabled setting should be able to be saved.');
231 }
232
233 private function addThisCustomConfigurationCodeShouldBeAbleToBeSaved($customConfigurationCode) {
234 $this->assertFieldByName(AddThis::CUSTOM_CONFIGURATION_CODE_KEY, $customConfigurationCode,
235 'AddThis custom configuration code should be able to be saved.');
236 }
237
238 private function addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor) {
239 $this->assertFieldByName(AddThis::UI_HEADER_COLOR_KEY, $uiHeaderColor,
240 'AddThis UI header color should be able to be saved.');
241 }
242
243 private function addThisUiDelayShouldBeAbleToBeSaved($uiDelay) {
244 $this->assertFieldByName(AddThis::UI_DELAY_KEY, $uiDelay,
245 'AddThis UI delay should be able to be saved.');
246 }
247
248 private function addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor) {
249 $this->assertFieldByName(AddThis::UI_HEADER_BACKGROUND_COLOR_KEY, $uiHeaderBackgroundColor,
250 'AddThis UI header background color should be able to be saved.');
251 }
252
253 private function addThisCoBrandShouldBeAbleToBeSaved($coBrand) {
254 $this->assertFieldByName(AddThis::CO_BRAND_KEY, $coBrand,
255 'AddThis co-brand should be able to be saved.');
256 }
257
258 private function addThisEnabledServicesShouldBeAbleToBeSaved($serviceName) {
259 $this->assertFieldChecked('edit-addthis-enabled-services-' . $serviceName,
260 'AddThis enabled services should be able to be saved.');
261 }
262
263 private function createAndLoginUser() {
264 $this->user = $this->createAdminUser();
265 $this->drupalLogin($this->user);
266 }
267
268 private function createAdminUser() {
269 return $this->drupalCreateUser(
270 array(
271 AddThis::PERMISSION_ADMINISTER_ADDTHIS,
272 AddThis::PERMISSION_ADMINISTER_ADVANCED_ADDTHIS,
273 'administer blocks',
274 'administer content types',
275 'administer nodes'
276 )
277 );
278 }
279 }
280
281 class AddThisPermissionsTestCase extends DrupalWebTestCase {
282
283 public function setUp() {
284 parent::setUp(AddThis::MODULE_NAME);
285 }
286
287 public function getInfo() {
288 return array(
289 'name' => 'AddThis: Permission tests',
290 'description' => 'Permission tests for the AddThis-module.',
291 'group' => 'AddThis',
292 );
293 }
294
295 public function testUserWithoutAdministerAddThisPermissionShouldNotBeAllowedToAccessAddThisSystemSettings() {
296 $this->drupalLogin($this->createAdminUserWithoutAddThisAdministrationPermission());
297 $this->drupalGet('admin/config/system/addthis');
298 $this->assertRaw(t('Access denied'),
299 'A user without administer addthis permission should not be able to access AddThis system settings.');
300 }
301
302 public function testUserWithoutAdministerAdvancedAddThisPermissionShouldNotBeAllowedToAccessAdvancedAddThisSystemSettings() {
303 $this->drupalLogin($this->createAdminUserWithoutAdvancedAddThisAdministrationPermission());
304 $this->drupalGet('admin/config/system/addthis');
305 $this->assertNoRaw(t('Advanced settings'),
306 'A user without administer advanced addthis permission should not be able to access advanced AddThis system settings.');
307 }
308
309 private function createAdminUserWithoutAddThisAdministrationPermission() {
310 return $this->drupalCreateUser(array('administer content types'));
311 }
312
313 private function createAdminUserWithoutAdvancedAddThisAdministrationPermission() {
314 return $this->drupalCreateUser(array('administer content types', AddThis::PERMISSION_ADMINISTER_ADDTHIS));
315 }
316 }
317
318 class TestHelper {
319
320 public static function stringContains($string, $contains) {
321 return strpos($string, $contains) !== FALSE;
322 }
323
324 public static function generateRandomLowercaseString() {
325 return drupal_strtolower(DrupalWebTestCase::randomName());
326 }
327 }