summaryrefslogtreecommitdiffstats
path: root/addthis.test
blob: b8eb46df0a09b043f7c5c0af6ac65a0765fbd203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php

/**
 * @file
 * Tests for the AddThis-module.
 */

class AddThisFunctionalityTestCase extends DrupalWebTestCase {

  private $user;

  public function setUp() {
    parent::setUp(AddThis::MODULE_NAME);
    $this->createAndLoginUser();
  }

  public function getInfo() {
    return array(
      'name' => 'AddThis: Functionality tests',
      'description' => 'Functionality tests for the AddThis-module.',
      'group' => 'AddThis',
    );
  }

  public function testFunctionality() {
    $this->addThisShouldProvideHelp();

    $this->addThisShouldDefineField();

    $this->addThisShouldDefineBlock();

    $this->addThisShouldBeAbleToBeUsedAsField();

    $this->addThisShouldBeAbleToBeUsedAsBlock();

    $this->addThisSystemSettingsShouldBeAbleToBeSaved();
  }

  private function addThisShouldProvideHelp() {
    $helpText = module_invoke(AddThis::MODULE_NAME, 'help', 'admin/help#addthis', NULL);
    $this->assertTrue(TestHelper::stringContains($helpText, t('About')), 'AddThis-module should provide help.');
  }

  private function addThisShouldDefineField() {
    $fieldInfo = module_invoke(AddThis::MODULE_NAME, 'field_info');
    $this->assertTrue(is_array($fieldInfo), t('AddThis-module should define a field.'));
  }

  private function addThisShouldDefineBlock() {
    $blockInfo = module_invoke(AddThis::MODULE_NAME, 'block_info');
    $this->assertTrue(is_array($blockInfo), t('AddThis-module should define a block.'));
  }

  private function addThisShouldBeAbleToBeUsedAsField() {
    $edit = array();
    $label = TestHelper::generateRandomLowercaseString();
    $edit["fields[_add_new_field][label]"] = $label;
    $edit["fields[_add_new_field][field_name]"] = $label;
    $edit["fields[_add_new_field][type]"] = AddThis::FIELD_TYPE;
    $edit["fields[_add_new_field][widget_type]"] = AddThis::WIDGET_TYPE;

    $this->drupalPost('admin/structure/types/manage/article/fields', $edit, t('Save'));

    $this->assertText($label, 'AddThis-module should be able to be added as a field.');

    $this->addThisShouldNotHaveConfigurableFieldLevelSettings($label);

    $this->drupalGet('admin/structure/types/manage/article/display');

    $this->addThisShouldProvideWidgetTypeCompactButton();

    $this->addThisShouldProvideWidgetTypeDisabled();

    $this->addThisShouldProvideWidgetTypeLargeButton();

    $this->addThisShouldProvideWidgetTypeSharecount();

    $this->addThisShouldProvideWidgetTypeToolbox();
  }

  private function addThisShouldNotHaveConfigurableFieldLevelSettings($label) {
    $this->assertText(t("@name has no field settings.", array('@name' => $label)),
                     'AddThis-module should not have configurable field level settings.');
  }

  private function addThisShouldProvideWidgetTypeCompactButton() {
    $this->assertText('Compact button', 'AddThis-module should provide widget type "Compact button"');
  }

  private function addThisShouldProvideWidgetTypeDisabled() {
    $this->assertText('Disabled', 'AddThis-module should provide widget type "Disabled"');
  }

  private function addThisShouldProvideWidgetTypeLargeButton() {
    $this->assertText('Large button', 'AddThis-module should provide widget type "Large button"');
  }

  private function addThisShouldProvideWidgetTypeSharecount() {
    $this->assertText('Sharecount', 'AddThis-module should provide widget type "Sharecount"');
  }

  private function addThisShouldProvideWidgetTypeToolbox() {
    $this->assertText('Toolbox', 'AddThis-module should provide widget type "Toolbox"');
  }

  private function addThisShouldBeAbleToBeUsedAsBlock() {
    $this->drupalGet('admin/structure/block');
    $this->assertRaw(t('AddThis'), 'AddThis should be able to be used as a block.');

    $this->addThisBlockSettingsShouldBeAbleToBeSaved();
  }

  private function addThisBlockSettingsShouldBeAbleToBeSaved() {
    $edit = array();
    $edit[AddThis::BLOCK_WIDGET_TYPE_KEY] = AddThis::WIDGET_TYPE_COMPACT_BUTTON;

    $this->drupalPost('admin/structure/block/manage/addthis/addthis_block/configure', $edit, t('Save block'));

    $this->assertText(t('The block configuration has been saved.'), 'AddThis block settings should be able to be saved.');
  }

  private function addThisSystemSettingsShouldBeAbleToBeSaved() {
    $edit = array();

    $profileId = TestHelper::generateRandomLowercaseString();
    $edit[AddThis::PROFILE_ID_KEY] = $profileId;

    $servicesCssUrl = AddThis::DEFAULT_SERVICES_CSS_URL;
    $edit[AddThis::SERVICES_CSS_URL_KEY] = $servicesCssUrl;

    $servicesJsonUrl = AddThis::DEFAULT_SERVICES_JSON_URL;
    $edit[AddThis::SERVICES_JSON_URL_KEY] = $servicesJsonUrl;

    $widgetJsUrl = AddThis::DEFAULT_WIDGET_JS_URL;
    $edit[AddThis::WIDGET_JS_URL_KEY] = $widgetJsUrl;

    $uiDelay = 400;
    $edit[AddThis::UI_DELAY_KEY] = $uiDelay;

    $uiHeaderBackgroundColor = '#000000';
    $edit[AddThis::UI_HEADER_BACKGROUND_COLOR_KEY] = $uiHeaderBackgroundColor;

    $uiHeaderColor = '#ffffff';
    $edit[AddThis::UI_HEADER_COLOR_KEY] = $uiHeaderColor;

    $coBrand = TestHelper::generateRandomLowercaseString();
    $edit[AddThis::CO_BRAND_KEY] = $coBrand;

    $edit[AddThis::LARGE_ICONS_ENABLED_KEY] = TRUE;

    $edit[AddThis::COMPLIANT_508_KEY] = TRUE;

    $edit[AddThis::CLICKBACK_TRACKING_ENABLED_KEY] = TRUE;
    
    $edit[AddThis::GOOGLE_ANALYTICS_TRACKING_ENABLED_KEY] = TRUE;
    
    $edit[AddThis::GOOGLE_ANALYTICS_SOCIAL_TRACKING_ENABLED_KEY] = TRUE;

    $edit[AddThis::ADDRESSBOOK_ENABLED_KEY] = TRUE;

    $edit[AddThis::CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY] = TRUE;

    $edit[AddThis::OPEN_WINDOWS_ENABLED_KEY] = TRUE;

    $edit[AddThis::STANDARD_CSS_ENABLED_KEY] = TRUE;

    $edit[AddThis::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY] = TRUE;

    $customConfigurationCode = TestHelper::generateRandomLowercaseString();
    $edit[AddThis::CUSTOM_CONFIGURATION_CODE_KEY] = $customConfigurationCode;

    $serviceName = '2linkme';
    $edit["addthis_enabled_services[$serviceName]"] = $serviceName;

    $this->drupalPost('admin/config/user-interface/addthis', $edit, t('Save configuration'));

    $this->assertText(t('The configuration options have been saved.'), 'AddThis system settings should be able to be saved.');

    $this->addThisProfileIdShouldBeAbleToBeSaved($profileId);

    $this->addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl);

    $this->addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl);

    $this->addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl);

    $this->addThisUiDelayShouldBeAbleToBeSaved($uiDelay);

    $this->addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor);

    $this->addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor);

    $this->addThisCoBrandShouldBeAbleToBeSaved($coBrand);

    $this->addThisLargeToolboxIconsSettingShouldBeAbleToBeSaved();

    $this->addThis508CompliantSettingShouldBeAbleToBeSaved();

    $this->addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved();

    $this->addThisGoogleAnalyticsTrackingEnabledSettingShouldBeAbleToBeSaved();

    $this->addThisGoogleAnalyticsSocialTrackingEnabledSettingShouldBeAbleToBeSaved();

    $this->addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved();

    $this->addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved();

    $this->addThisStandardCssEnabledSettingShouldBeAbleToBeSaved();

    $this->addThisEnabledServicesShouldBeAbleToBeSaved($serviceName);

    $this->addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved();
  }

  private function addThisProfileIdShouldBeAbleToBeSaved($profileId) {
    $this->assertFieldByName(AddThis::PROFILE_ID_KEY, $profileId, 'AddThis profile ID should be able to be saved.');
  }

  private function addThisServicesCssUrlShouldBeAbleToBeSaved($servicesCssUrl) {
    $this->assertFieldByName(AddThis::SERVICES_CSS_URL_KEY, $servicesCssUrl,
                             'AddThis services stylesheet URL should be able to be saved.');
  }

  private function addThisServicesJsonUrlShouldBeAbleToBeSaved($servicesJsonUrl) {
    $this->assertFieldByName(AddThis::SERVICES_JSON_URL_KEY, $servicesJsonUrl,
                             'AddThis services json URL should be able to be saved.');
  }

  private function addThisWidgetJsUrlShouldBeAbleToBeSaved($widgetJsUrl) {
    $this->assertFieldByName(AddThis::WIDGET_JS_URL_KEY, $widgetJsUrl,
                             'AddThis javascript widget URL should be able to be saved.');
  }

  private function addThisLargeToolboxIconsSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-large-icons-enabled',
                              'AddThis large toolbox icons setting should be able to be saved.');
  }

  private function addThis508CompliantSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-508-compliant',
                              'AddThis 508 compliant setting should be able to be saved.');
  }

  private function addThisClickbackTrackingEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-clickback-tracking-enabled',
                              'AddThis clickback tracking enabled setting should be able to be saved.');
  }

  private function addThisGoogleAnalyticsTrackingEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-google-analytics-tracking-enabled',
                              'AddThis Google Analytics tracking enabled setting should be able to be saved.');
  }

  private function addThisGoogleAnalyticsSocialTrackingEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-google-analytics-tracking-enabled',
                              'AddThis Google Analytics social tracking enabled setting should be able to be saved.');
  }

  private function addThisClickToOpenCompactMenuEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-click-to-open-compact-menu-enabled',
                              'AddThis click to open compact menu setting should be able to be saved.');
  }

  private function addThisOpenWindowsEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-open-windows-enabled',
                              'AddThis open windows setting should be able to be saved.');
  }

  private function addThisStandardCssEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-open-windows-enabled',
                              'AddThis open windows setting should be able to be saved.');
  }

  private function addThisFacebookLikeEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-facebook-like-enabled',
                              'AddThis Facebook Like enabled setting should be able to be saved.');
  }

  private function addThisGooglePlusOneEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-google-plus-one-enabled',
                              'AddThis Google +1 enabled setting should be able to be saved.');
  }

  private function addThisTwitterEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-twitter-enabled',
                              'AddThis Twitter enabled setting should be able to be saved.');
  }

  private function addThisAddressbookEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-addressbook-enabled',
                              'AddThis addressbook enabled setting should be able to be saved.');
  }

  private function addThisNumberOfPreferredServicesShouldBeAbleToBeSaved($numberOfPreferredServices) {
    $this->assertFieldByName(AddThis::NUMBER_OF_PREFERRED_SERVICES_KEY, $numberOfPreferredServices,
                             'The number of preferred services should be able to be saved.');
  }

  private function addThisCustomConfigurationCodeEnabledSettingShouldBeAbleToBeSaved() {
    $this->assertFieldChecked('edit-addthis-custom-configuration-code-enabled',
                              'AddThis custom configuration code enabled setting should be able to be saved.');
  }

  private function addThisCustomConfigurationCodeShouldBeAbleToBeSaved($customConfigurationCode) {
    $this->assertFieldByName(AddThis::CUSTOM_CONFIGURATION_CODE_KEY, $customConfigurationCode,
                             'AddThis custom configuration code should be able to be saved.');
  }

  private function addThisUiHeaderColorShouldBeAbleToBeSaved($uiHeaderColor) {
    $this->assertFieldByName(AddThis::UI_HEADER_COLOR_KEY, $uiHeaderColor,
                             'AddThis UI header color should be able to be saved.');
  }

  private function addThisUiDelayShouldBeAbleToBeSaved($uiDelay) {
    $this->assertFieldByName(AddThis::UI_DELAY_KEY, $uiDelay,
                             'AddThis UI delay should be able to be saved.');
  }

  private function addThisUiHeaderBackgroundColorShouldBeAbleToBeSaved($uiHeaderBackgroundColor) {
    $this->assertFieldByName(AddThis::UI_HEADER_BACKGROUND_COLOR_KEY, $uiHeaderBackgroundColor,
                             'AddThis UI header background color should be able to be saved.');
  }

  private function addThisCoBrandShouldBeAbleToBeSaved($coBrand) {
    $this->assertFieldByName(AddThis::CO_BRAND_KEY, $coBrand,
                             'AddThis co-brand should be able to be saved.');
  }

  private function addThisEnabledServicesShouldBeAbleToBeSaved($serviceName) {
    $this->assertFieldChecked('edit-addthis-enabled-services-' . $serviceName,
                              'AddThis enabled services should be able to be saved.');
  }

  private function createAndLoginUser() {
    $this->user = $this->createAdminUser();
    $this->drupalLogin($this->user);
  }

  private function createAdminUser() {
    return $this->drupalCreateUser(
      array(
        AddThis::PERMISSION_ADMINISTER_ADDTHIS,
        AddThis::PERMISSION_ADMINISTER_ADVANCED_ADDTHIS,
        'administer blocks',
        'administer content types',
        'administer nodes'
      )
    );
  }
}

class AddThisPermissionsTestCase extends DrupalWebTestCase {

  public function setUp() {
    parent::setUp(AddThis::MODULE_NAME);
  }

  public function getInfo() {
    return array(
      'name' => 'AddThis: Permission tests',
      'description' => 'Permission tests for the AddThis-module.',
      'group' => 'AddThis',
    );
  }

  public function testUserWithoutAdministerAddThisPermissionShouldNotBeAllowedToAccessAddThisSystemSettings() {
    $this->drupalLogin($this->createAdminUserWithoutAddThisAdministrationPermission());
    $this->drupalGet('admin/config/system/addthis');
    $this->assertRaw(t('Access denied'),
                     'A user without administer addthis permission should not be able to access AddThis system settings.');
  }

  public function testUserWithoutAdministerAdvancedAddThisPermissionShouldNotBeAllowedToAccessAdvancedAddThisSystemSettings() {
    $this->drupalLogin($this->createAdminUserWithoutAdvancedAddThisAdministrationPermission());
    $this->drupalGet('admin/config/system/addthis');
    $this->assertNoRaw(t('Advanced settings'),
                       'A user without administer advanced addthis permission should not be able to access advanced AddThis system settings.');
  }

  private function createAdminUserWithoutAddThisAdministrationPermission() {
    return $this->drupalCreateUser(array('administer content types'));
  }

  private function createAdminUserWithoutAdvancedAddThisAdministrationPermission() {
    return $this->drupalCreateUser(array('administer content types', AddThis::PERMISSION_ADMINISTER_ADDTHIS));
  }
}

class TestHelper {

  public static function stringContains($string, $contains) {
    return strpos($string, $contains) !== FALSE;
  }

  public static function generateRandomLowercaseString() {
    return drupal_strtolower(DrupalWebTestCase::randomName());
  }
}