1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains EntityReferenceHandlersTestCase
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Test for Entity Reference handlers.
|
10
|
*/
|
11
|
class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
|
12
|
public static function getInfo() {
|
13
|
return array(
|
14
|
'name' => 'Entity Reference Handlers',
|
15
|
'description' => 'Tests for the base handlers provided by Entity Reference.',
|
16
|
'group' => 'Entity Reference',
|
17
|
);
|
18
|
}
|
19
|
|
20
|
public function setUp() {
|
21
|
parent::setUp('entityreference');
|
22
|
}
|
23
|
|
24
|
protected function assertReferencable($field, $tests, $handler_name) {
|
25
|
$handler = entityreference_get_selection_handler($field);
|
26
|
|
27
|
foreach ($tests as $test) {
|
28
|
foreach ($test['arguments'] as $arguments) {
|
29
|
$result = call_user_func_array(array($handler, 'getReferencableEntities'), $arguments);
|
30
|
$this->assertEqual($result, $test['result'], format_string('Valid result set returned by @handler.', array('@handler' => $handler_name)));
|
31
|
|
32
|
$result = call_user_func_array(array($handler, 'countReferencableEntities'), $arguments);
|
33
|
if (!empty($test['result'])) {
|
34
|
$bundle = key($test['result']);
|
35
|
$count = count($test['result'][$bundle]);
|
36
|
}
|
37
|
else {
|
38
|
$count = 0;
|
39
|
}
|
40
|
|
41
|
$this->assertEqual($result, $count, format_string('Valid count returned by @handler.', array('@handler' => $handler_name)));
|
42
|
}
|
43
|
}
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Test the node-specific overrides of the entity handler.
|
48
|
*/
|
49
|
public function testNodeHandler() {
|
50
|
// Build a fake field instance.
|
51
|
$field = array(
|
52
|
'translatable' => FALSE,
|
53
|
'entity_types' => array(),
|
54
|
'settings' => array(
|
55
|
'handler' => 'base',
|
56
|
'target_type' => 'node',
|
57
|
'handler_settings' => array(
|
58
|
'target_bundles' => array(),
|
59
|
),
|
60
|
),
|
61
|
'field_name' => 'test_field',
|
62
|
'type' => 'entityreference',
|
63
|
'cardinality' => '1',
|
64
|
);
|
65
|
|
66
|
// Build a set of test data.
|
67
|
// Titles contain HTML-special characters to test escaping.
|
68
|
$nodes = array(
|
69
|
'published1' => (object) array(
|
70
|
'type' => 'article',
|
71
|
'status' => 1,
|
72
|
'title' => 'Node published1 (<&>)',
|
73
|
'uid' => 1,
|
74
|
),
|
75
|
'published2' => (object) array(
|
76
|
'type' => 'article',
|
77
|
'status' => 1,
|
78
|
'title' => 'Node published2 (<&>)',
|
79
|
'uid' => 1,
|
80
|
),
|
81
|
'unpublished' => (object) array(
|
82
|
'type' => 'article',
|
83
|
'status' => 0,
|
84
|
'title' => 'Node unpublished (<&>)',
|
85
|
'uid' => 1,
|
86
|
),
|
87
|
// Title purposefully starts with same characters as published1 and
|
88
|
// published2 above but contains a slash.
|
89
|
'published_withslash' => (object) array(
|
90
|
'type' => 'article',
|
91
|
'status' => 1,
|
92
|
'title' => 'Node pub/lished1',
|
93
|
'uid' => 1,
|
94
|
),
|
95
|
);
|
96
|
|
97
|
$node_labels = array();
|
98
|
foreach ($nodes as $key => $node) {
|
99
|
node_save($node);
|
100
|
$node_labels[$key] = check_plain($node->title);
|
101
|
}
|
102
|
|
103
|
// Test as a non-admin.
|
104
|
$normal_user = $this->drupalCreateUser(array('access content'));
|
105
|
$GLOBALS['user'] = $normal_user;
|
106
|
$referencable_tests = array(
|
107
|
array(
|
108
|
'arguments' => array(
|
109
|
array(NULL, 'CONTAINS'),
|
110
|
),
|
111
|
'result' => array(
|
112
|
'article' => array(
|
113
|
$nodes['published1']->nid => $node_labels['published1'],
|
114
|
$nodes['published2']->nid => $node_labels['published2'],
|
115
|
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
|
116
|
),
|
117
|
),
|
118
|
),
|
119
|
array(
|
120
|
'arguments' => array(
|
121
|
array('published1', 'CONTAINS'),
|
122
|
array('Published1', 'CONTAINS'),
|
123
|
),
|
124
|
'result' => array(
|
125
|
'article' => array(
|
126
|
$nodes['published1']->nid => $node_labels['published1'],
|
127
|
),
|
128
|
),
|
129
|
),
|
130
|
array(
|
131
|
'arguments' => array(
|
132
|
array('published2', 'CONTAINS'),
|
133
|
array('Published2', 'CONTAINS'),
|
134
|
),
|
135
|
'result' => array(
|
136
|
'article' => array(
|
137
|
$nodes['published2']->nid => $node_labels['published2'],
|
138
|
),
|
139
|
),
|
140
|
),
|
141
|
array(
|
142
|
'arguments' => array(
|
143
|
array('invalid node', 'CONTAINS'),
|
144
|
),
|
145
|
'result' => array(),
|
146
|
),
|
147
|
array(
|
148
|
'arguments' => array(
|
149
|
array('Node unpublished', 'CONTAINS'),
|
150
|
),
|
151
|
'result' => array(),
|
152
|
),
|
153
|
// Searching for "Node pub/" should return only the published_withslash node
|
154
|
// and not published1 and published2 from above.
|
155
|
array(
|
156
|
'arguments' => array(
|
157
|
array('Node pub/', 'CONTAINS'),
|
158
|
),
|
159
|
'result' => array(
|
160
|
'article' => array(
|
161
|
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
|
162
|
),
|
163
|
),
|
164
|
),
|
165
|
);
|
166
|
$this->assertReferencable($field, $referencable_tests, 'Node handler');
|
167
|
|
168
|
// Test as an admin.
|
169
|
$admin_user = $this->drupalCreateUser(array('access content', 'bypass node access'));
|
170
|
$GLOBALS['user'] = $admin_user;
|
171
|
$referencable_tests = array(
|
172
|
array(
|
173
|
'arguments' => array(
|
174
|
array(NULL, 'CONTAINS'),
|
175
|
),
|
176
|
'result' => array(
|
177
|
'article' => array(
|
178
|
$nodes['published1']->nid => $node_labels['published1'],
|
179
|
$nodes['published2']->nid => $node_labels['published2'],
|
180
|
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
|
181
|
$nodes['unpublished']->nid => $node_labels['unpublished'],
|
182
|
),
|
183
|
),
|
184
|
),
|
185
|
array(
|
186
|
'arguments' => array(
|
187
|
array('Node unpublished', 'CONTAINS'),
|
188
|
),
|
189
|
'result' => array(
|
190
|
'article' => array(
|
191
|
$nodes['unpublished']->nid => $node_labels['unpublished'],
|
192
|
),
|
193
|
),
|
194
|
),
|
195
|
);
|
196
|
$this->assertReferencable($field, $referencable_tests, 'Node handler (admin)');
|
197
|
|
198
|
// Verify autocomplete input validation.
|
199
|
$handler = entityreference_get_selection_handler($field);
|
200
|
$element = array(
|
201
|
'#parents' => array('element_name'),
|
202
|
);
|
203
|
$form_state = array();
|
204
|
$form = array();
|
205
|
$value = $handler->validateAutocompleteInput($nodes['published1']->title, $element, $form_state, $form);
|
206
|
$this->assertEqual($value, $nodes['published1']->nid);
|
207
|
|
208
|
$invalid_input = $this->randomName();
|
209
|
$value = $handler->validateAutocompleteInput($invalid_input, $element, $form_state, $form);
|
210
|
$this->assertNull($value);
|
211
|
$this->assertEqual(form_get_error($element), t('There are no entities matching "%value"', array('%value' => $invalid_input)));
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Test the user-specific overrides of the entity handler.
|
216
|
*/
|
217
|
public function testUserHandler() {
|
218
|
// Build a fake field instance.
|
219
|
$field = array(
|
220
|
'translatable' => FALSE,
|
221
|
'entity_types' => array(),
|
222
|
'settings' => array(
|
223
|
'handler' => 'base',
|
224
|
'target_type' => 'user',
|
225
|
'handler_settings' => array(
|
226
|
'target_bundles' => array(),
|
227
|
),
|
228
|
),
|
229
|
'field_name' => 'test_field',
|
230
|
'type' => 'entityreference',
|
231
|
'cardinality' => '1',
|
232
|
);
|
233
|
|
234
|
// Build a set of test data.
|
235
|
$users = array(
|
236
|
'anonymous' => user_load(0),
|
237
|
'admin' => user_load(1),
|
238
|
'non_admin' => (object) array(
|
239
|
'name' => 'non_admin <&>',
|
240
|
'mail' => 'non_admin@example.com',
|
241
|
'roles' => array(),
|
242
|
'pass' => user_password(),
|
243
|
'status' => 1,
|
244
|
),
|
245
|
'blocked' => (object) array(
|
246
|
'name' => 'blocked <&>',
|
247
|
'mail' => 'blocked@example.com',
|
248
|
'roles' => array(),
|
249
|
'pass' => user_password(),
|
250
|
'status' => 0,
|
251
|
),
|
252
|
);
|
253
|
|
254
|
// The label of the anonymous user is variable_get('anonymous').
|
255
|
$users['anonymous']->name = variable_get('anonymous', t('Anonymous'));
|
256
|
|
257
|
$user_labels = array();
|
258
|
foreach ($users as $key => $user) {
|
259
|
if (!isset($user->uid)) {
|
260
|
$users[$key] = $user = user_save(drupal_anonymous_user(), (array) $user);
|
261
|
}
|
262
|
$user_labels[$key] = check_plain($user->name);
|
263
|
}
|
264
|
|
265
|
// Test as a non-admin.
|
266
|
$GLOBALS['user'] = $users['non_admin'];
|
267
|
$referencable_tests = array(
|
268
|
array(
|
269
|
'arguments' => array(
|
270
|
array(NULL, 'CONTAINS'),
|
271
|
),
|
272
|
'result' => array(
|
273
|
'user' => array(
|
274
|
$users['admin']->uid => '- Restricted access -',
|
275
|
$users['non_admin']->uid => $user_labels['non_admin'],
|
276
|
),
|
277
|
),
|
278
|
),
|
279
|
array(
|
280
|
'arguments' => array(
|
281
|
array('non_admin', 'CONTAINS'),
|
282
|
array('NON_ADMIN', 'CONTAINS'),
|
283
|
),
|
284
|
'result' => array(
|
285
|
'user' => array(
|
286
|
$users['non_admin']->uid => $user_labels['non_admin'],
|
287
|
),
|
288
|
),
|
289
|
),
|
290
|
array(
|
291
|
'arguments' => array(
|
292
|
array('invalid user', 'CONTAINS'),
|
293
|
),
|
294
|
'result' => array(),
|
295
|
),
|
296
|
array(
|
297
|
'arguments' => array(
|
298
|
array('blocked', 'CONTAINS'),
|
299
|
),
|
300
|
'result' => array(),
|
301
|
),
|
302
|
);
|
303
|
$this->assertReferencable($field, $referencable_tests, 'User handler');
|
304
|
|
305
|
$GLOBALS['user'] = $users['admin'];
|
306
|
$referencable_tests = array(
|
307
|
array(
|
308
|
'arguments' => array(
|
309
|
array(NULL, 'CONTAINS'),
|
310
|
),
|
311
|
'result' => array(
|
312
|
'user' => array(
|
313
|
$users['anonymous']->uid => $user_labels['anonymous'],
|
314
|
$users['admin']->uid => $user_labels['admin'],
|
315
|
$users['non_admin']->uid => $user_labels['non_admin'],
|
316
|
$users['blocked']->uid => $user_labels['blocked'],
|
317
|
),
|
318
|
),
|
319
|
),
|
320
|
array(
|
321
|
'arguments' => array(
|
322
|
array('blocked', 'CONTAINS'),
|
323
|
),
|
324
|
'result' => array(
|
325
|
'user' => array(
|
326
|
$users['blocked']->uid => $user_labels['blocked'],
|
327
|
),
|
328
|
),
|
329
|
),
|
330
|
array(
|
331
|
'arguments' => array(
|
332
|
array('Anonymous', 'CONTAINS'),
|
333
|
array('anonymous', 'CONTAINS'),
|
334
|
),
|
335
|
'result' => array(
|
336
|
'user' => array(
|
337
|
$users['anonymous']->uid => $user_labels['anonymous'],
|
338
|
),
|
339
|
),
|
340
|
),
|
341
|
);
|
342
|
$this->assertReferencable($field, $referencable_tests, 'User handler (admin)');
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* Test the comment-specific overrides of the entity handler.
|
347
|
*/
|
348
|
public function testCommentHandler() {
|
349
|
// Build a fake field instance.
|
350
|
$field = array(
|
351
|
'translatable' => FALSE,
|
352
|
'entity_types' => array(),
|
353
|
'settings' => array(
|
354
|
'handler' => 'base',
|
355
|
'target_type' => 'comment',
|
356
|
'handler_settings' => array(
|
357
|
'target_bundles' => array(),
|
358
|
),
|
359
|
),
|
360
|
'field_name' => 'test_field',
|
361
|
'type' => 'entityreference',
|
362
|
'cardinality' => '1',
|
363
|
);
|
364
|
|
365
|
// Build a set of test data.
|
366
|
$nodes = array(
|
367
|
'published' => (object) array(
|
368
|
'type' => 'article',
|
369
|
'status' => 1,
|
370
|
'title' => 'Node published',
|
371
|
'uid' => 1,
|
372
|
),
|
373
|
'unpublished' => (object) array(
|
374
|
'type' => 'article',
|
375
|
'status' => 0,
|
376
|
'title' => 'Node unpublished',
|
377
|
'uid' => 1,
|
378
|
),
|
379
|
);
|
380
|
foreach ($nodes as $node) {
|
381
|
node_save($node);
|
382
|
}
|
383
|
|
384
|
$comments = array(
|
385
|
'published_published' => (object) array(
|
386
|
'nid' => $nodes['published']->nid,
|
387
|
'uid' => 1,
|
388
|
'cid' => NULL,
|
389
|
'pid' => 0,
|
390
|
'status' => COMMENT_PUBLISHED,
|
391
|
'subject' => 'Comment Published <&>',
|
392
|
'hostname' => ip_address(),
|
393
|
'language' => LANGUAGE_NONE,
|
394
|
),
|
395
|
'published_unpublished' => (object) array(
|
396
|
'nid' => $nodes['published']->nid,
|
397
|
'uid' => 1,
|
398
|
'cid' => NULL,
|
399
|
'pid' => 0,
|
400
|
'status' => COMMENT_NOT_PUBLISHED,
|
401
|
'subject' => 'Comment Unpublished <&>',
|
402
|
'hostname' => ip_address(),
|
403
|
'language' => LANGUAGE_NONE,
|
404
|
),
|
405
|
'unpublished_published' => (object) array(
|
406
|
'nid' => $nodes['unpublished']->nid,
|
407
|
'uid' => 1,
|
408
|
'cid' => NULL,
|
409
|
'pid' => 0,
|
410
|
'status' => COMMENT_NOT_PUBLISHED,
|
411
|
'subject' => 'Comment Published on Unpublished node <&>',
|
412
|
'hostname' => ip_address(),
|
413
|
'language' => LANGUAGE_NONE,
|
414
|
),
|
415
|
);
|
416
|
|
417
|
$comment_labels = array();
|
418
|
foreach ($comments as $key => $comment) {
|
419
|
comment_save($comment);
|
420
|
$comment_labels[$key] = check_plain($comment->subject);
|
421
|
}
|
422
|
|
423
|
// Test as a non-admin.
|
424
|
$normal_user = $this->drupalCreateUser(array('access content', 'access comments'));
|
425
|
$GLOBALS['user'] = $normal_user;
|
426
|
$referencable_tests = array(
|
427
|
array(
|
428
|
'arguments' => array(
|
429
|
array(NULL, 'CONTAINS'),
|
430
|
),
|
431
|
'result' => array(
|
432
|
'comment_node_article' => array(
|
433
|
$comments['published_published']->cid => $comment_labels['published_published'],
|
434
|
),
|
435
|
),
|
436
|
),
|
437
|
array(
|
438
|
'arguments' => array(
|
439
|
array('Published', 'CONTAINS'),
|
440
|
),
|
441
|
'result' => array(
|
442
|
'comment_node_article' => array(
|
443
|
$comments['published_published']->cid => $comment_labels['published_published'],
|
444
|
),
|
445
|
),
|
446
|
),
|
447
|
array(
|
448
|
'arguments' => array(
|
449
|
array('invalid comment', 'CONTAINS'),
|
450
|
),
|
451
|
'result' => array(),
|
452
|
),
|
453
|
array(
|
454
|
'arguments' => array(
|
455
|
array('Comment Unpublished', 'CONTAINS'),
|
456
|
),
|
457
|
'result' => array(),
|
458
|
),
|
459
|
);
|
460
|
$this->assertReferencable($field, $referencable_tests, 'Comment handler');
|
461
|
|
462
|
// Test as a comment admin.
|
463
|
$admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments'));
|
464
|
$GLOBALS['user'] = $admin_user;
|
465
|
$referencable_tests = array(
|
466
|
array(
|
467
|
'arguments' => array(
|
468
|
array(NULL, 'CONTAINS'),
|
469
|
),
|
470
|
'result' => array(
|
471
|
'comment_node_article' => array(
|
472
|
$comments['published_published']->cid => $comment_labels['published_published'],
|
473
|
$comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
|
474
|
),
|
475
|
),
|
476
|
),
|
477
|
);
|
478
|
$this->assertReferencable($field, $referencable_tests, 'Comment handler (comment admin)');
|
479
|
|
480
|
// Test as a node and comment admin.
|
481
|
$admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
|
482
|
$GLOBALS['user'] = $admin_user;
|
483
|
$referencable_tests = array(
|
484
|
array(
|
485
|
'arguments' => array(
|
486
|
array(NULL, 'CONTAINS'),
|
487
|
),
|
488
|
'result' => array(
|
489
|
'comment_node_article' => array(
|
490
|
$comments['published_published']->cid => $comment_labels['published_published'],
|
491
|
$comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
|
492
|
$comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
|
493
|
),
|
494
|
),
|
495
|
),
|
496
|
);
|
497
|
$this->assertReferencable($field, $referencable_tests, 'Comment handler (comment + node admin)');
|
498
|
}
|
499
|
|
500
|
/**
|
501
|
* Assert sorting by field works for non-admins.
|
502
|
*
|
503
|
* Since we are sorting on a field, we need to make sure the base-table
|
504
|
* is added, and access-control is behaving as expected.
|
505
|
*/
|
506
|
public function testSortByField() {
|
507
|
// Add text field to entity, to sort by.
|
508
|
$field_info = array(
|
509
|
'field_name' => 'field_text',
|
510
|
'type' => 'text',
|
511
|
'entity_types' => array('node'),
|
512
|
);
|
513
|
field_create_field($field_info);
|
514
|
|
515
|
$instance = array(
|
516
|
'label' => 'Text Field',
|
517
|
'field_name' => 'field_text',
|
518
|
'entity_type' => 'node',
|
519
|
'bundle' => 'article',
|
520
|
'settings' => array(),
|
521
|
'required' => FALSE,
|
522
|
);
|
523
|
field_create_instance($instance);
|
524
|
|
525
|
|
526
|
// Build a fake field instance.
|
527
|
$field = array(
|
528
|
'translatable' => FALSE,
|
529
|
'entity_types' => array(),
|
530
|
'settings' => array(
|
531
|
'handler' => 'base',
|
532
|
'target_type' => 'node',
|
533
|
'handler_settings' => array(
|
534
|
'target_bundles' => array(),
|
535
|
// Add sorting.
|
536
|
'sort' => array(
|
537
|
'type' => 'field',
|
538
|
'field' => 'field_text:value',
|
539
|
'direction' => 'DESC',
|
540
|
),
|
541
|
),
|
542
|
),
|
543
|
'field_name' => 'test_field',
|
544
|
'type' => 'entityreference',
|
545
|
'cardinality' => '1',
|
546
|
);
|
547
|
|
548
|
// Build a set of test data.
|
549
|
$nodes = array(
|
550
|
'published1' => (object) array(
|
551
|
'type' => 'article',
|
552
|
'status' => 1,
|
553
|
'title' => 'Node published1 (<&>)',
|
554
|
'uid' => 1,
|
555
|
'field_text' => array(
|
556
|
LANGUAGE_NONE => array(
|
557
|
array(
|
558
|
'value' => 1,
|
559
|
),
|
560
|
),
|
561
|
),
|
562
|
),
|
563
|
'published2' => (object) array(
|
564
|
'type' => 'article',
|
565
|
'status' => 1,
|
566
|
'title' => 'Node published2 (<&>)',
|
567
|
'uid' => 1,
|
568
|
'field_text' => array(
|
569
|
LANGUAGE_NONE => array(
|
570
|
array(
|
571
|
'value' => 2,
|
572
|
),
|
573
|
),
|
574
|
),
|
575
|
),
|
576
|
'unpublished' => (object) array(
|
577
|
'type' => 'article',
|
578
|
'status' => 0,
|
579
|
'title' => 'Node unpublished (<&>)',
|
580
|
'uid' => 1,
|
581
|
'field_text' => array(
|
582
|
LANGUAGE_NONE => array(
|
583
|
array(
|
584
|
'value' => 3,
|
585
|
),
|
586
|
),
|
587
|
),
|
588
|
),
|
589
|
);
|
590
|
|
591
|
$node_labels = array();
|
592
|
foreach ($nodes as $key => $node) {
|
593
|
node_save($node);
|
594
|
$node_labels[$key] = check_plain($node->title);
|
595
|
}
|
596
|
|
597
|
// Test as a non-admin.
|
598
|
$normal_user = $this->drupalCreateUser(array('access content'));
|
599
|
$GLOBALS['user'] = $normal_user;
|
600
|
|
601
|
$handler = entityreference_get_selection_handler($field);
|
602
|
|
603
|
// Not only assert the result, but make sure the keys are sorted as
|
604
|
// expected.
|
605
|
$result = $handler->getReferencableEntities();
|
606
|
$expected_result = array(
|
607
|
$nodes['published2']->nid => $node_labels['published2'],
|
608
|
$nodes['published1']->nid => $node_labels['published1'],
|
609
|
);
|
610
|
$this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values for non-admin.');
|
611
|
}
|
612
|
}
|