summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/common_test/common_test.module
blob: 175cd5ac0c3528ef0f32f978238713bff37757f7 (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
<?php

/**
 * @file
 * Helper module for the Common tests.
 */

/**
 * Implements hook_menu().
 */
function common_test_menu() {
  $items['common-test/destination'] = array(
    'title' => 'Drupal Get Destination',
    'page callback' => 'common_test_destination',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/query-string'] = array(
    'title' => 'Test querystring',
    'page callback' => 'common_test_js_and_css_querystring',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/drupal-render-invalid-keys'] = array(
    'title' => 'Drupal Render',
    'page callback' => 'common_test_drupal_render_invalid_keys',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Prints a destination query parameter.
 */
function common_test_destination() {
  $destination = drupal_get_destination();
  print "The destination: " . check_plain($destination['destination']);
}

/**
 * Renders an element with an invalid render array key.
 */
function common_test_drupal_render_invalid_keys() {
  define('SIMPLETEST_COLLECT_ERRORS', FALSE);

  // Keys that begin with # may contain a value of any type, otherwise they must
  // contain arrays.
  $key = 'child';
  $value = 'This should be an array.';
  $element = array(
    $key => $value,
  );
  return drupal_render($element);
}

/**
 * Applies #printed to an element to help test #pre_render.
 */
function common_test_drupal_render_printing_pre_render($elements) {
  $elements['#printed'] = TRUE;
  return $elements;
}

/**
 * Implements hook_TYPE_alter().
 */
function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  // Alter first argument.
  if (is_array($data)) {
    $data['foo'] = 'Drupal';
  }
  elseif (is_object($data)) {
    $data->foo = 'Drupal';
  }
  // Alter second argument, if present.
  if (isset($arg2)) {
    if (is_array($arg2)) {
      $arg2['foo'] = 'Drupal';
    }
    elseif (is_object($arg2)) {
      $arg2->foo = 'Drupal';
    }
  }
  // Try to alter third argument, if present.
  if (isset($arg3)) {
    if (is_array($arg3)) {
      $arg3['foo'] = 'Drupal';
    }
    elseif (is_object($arg3)) {
      $arg3->foo = 'Drupal';
    }
  }
}

/**
 * Implements hook_TYPE_alter() on behalf of Bartik theme.
 *
 * Same as common_test_drupal_alter_alter(), but here, we verify that themes
 * can also alter and come last.
 */
function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  // Alter first argument.
  if (is_array($data)) {
    $data['foo'] .= ' theme';
  }
  elseif (is_object($data)) {
    $data->foo .= ' theme';
  }
  // Alter second argument, if present.
  if (isset($arg2)) {
    if (is_array($arg2)) {
      $arg2['foo'] .= ' theme';
    }
    elseif (is_object($arg2)) {
      $arg2->foo .= ' theme';
    }
  }
  // Try to alter third argument, if present.
  if (isset($arg3)) {
    if (is_array($arg3)) {
      $arg3['foo'] .= ' theme';
    }
    elseif (is_object($arg3)) {
      $arg3->foo .= ' theme';
    }
  }
}

/**
 * Implements hook_TYPE_alter() on behalf of block module.
 *
 * This is for verifying that drupal_alter(array(TYPE1, TYPE2), ...) allows
 * hook_module_implements_alter() to affect the order in which module
 * implementations are executed.
 */
function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  $data['foo'] .= ' block';
}

/**
 * Implements hook_module_implements_alter().
 *
 * @see block_drupal_alter_foo_alter()
 */
function common_test_module_implements_alter(&$implementations, $hook) {
  // For drupal_alter(array('drupal_alter', 'drupal_alter_foo'), ...), make the
  // block module implementations run after all the other modules. Note that
  // when drupal_alter() is called with an array of types, the first type is
  // considered primary and controls the module order.
  if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
    $group = $implementations['block'];
    unset($implementations['block']);
    $implementations['block'] = $group;
  }
}

/**
 * Implements hook_theme().
 */
function common_test_theme() {
  return array(
    'common_test_foo' => array(
      'variables' => array('foo' => 'foo', 'bar' => 'bar'),
      'template' => 'common-test-foo',
    ),
    'common_test_empty' => array(
      'variables' => array('foo' => 'foo'),
    ),
  );
}

/**
 * Provides a theme function for drupal_render().
 */
function theme_common_test_foo($variables) {
  return $variables['foo'] . $variables['bar'];
}

/**
 * Always returns an empty string.
 */
function theme_common_test_empty($variables) {
  return '';
}

/**
 * Implements hook_library_info_alter().
 */
function common_test_library_info_alter(&$libraries, $module) {
  if ($module == 'system' && isset($libraries['jquery.farbtastic'])) {
    // Change the title of Farbtastic to "Farbtastic: Altered Library".
    $libraries['jquery.farbtastic']['title'] = 'Farbtastic: Altered Library';
    // Make Farbtastic depend on jQuery Form to test library dependencies.
    $libraries['jquery.farbtastic']['dependencies'][] = array('system', 'jquery.form');
  }
}

/**
 * Implements hook_library_info().
 *
 * Adds Farbtastic in a different version.
 */
function common_test_library_info() {
  $libraries['jquery.farbtastic'] = array(
    'title' => 'Custom Farbtastic Library',
    'website' => 'http://code.google.com/p/farbtastic/',
    'version' => '5.3',
    'js' => array(
      'core/misc/farbtastic/farbtastic.js' => array(),
    ),
    'css' => array(
      'core/misc/farbtastic/farbtastic.css' => array(),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
    ),
  );
  // Nominate a library using the shorthand format, where no options are given,
  // just the file name.
  $libraries['shorthand.plugin'] = array(
    'title' => 'Shorthand Plugin',
    'website' => 'http://www.example.com/',
    'version' => '0.8.3.37',
    'js' => array(
      // Here we attach the JavaScript file using the shorthand format, only
      // the file name is given, no options.
      drupal_get_path('module', 'common_test') . '/js/shorthand.js',
    ),
    'dependencies' => array(
      array('system', 'jquery'),
    ),
  );
  return $libraries;
}

/**
 * Adds a JavaScript file and a CSS file with a query string appended.
 */
function common_test_js_and_css_querystring() {
   drupal_add_library('system', 'drupalSettings');
   drupal_add_js(drupal_get_path('module', 'node') . '/node.js');
   drupal_add_css(drupal_get_path('module', 'node') . '/css/node.admin.css');
   // A relative URI may have a query string.
   drupal_add_css('/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2');
   return '';
}

/**
 * Implements hook_cron().
 *
 * System module should handle if a module does not catch an exception and keep
 * cron going.
 *
 * @see common_test_cron_helper()
 *
 */
function common_test_cron() {
  throw new Exception(t('Uncaught exception'));
}