Projet

Général

Profil

Paste
Télécharger (17,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entityreference / tests / entityreference.handlers.test @ 56aebcb7

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

    
199
  /**
200
   * Test the user-specific overrides of the entity handler.
201
   */
202
  public function testUserHandler() {
203
    // Build a fake field instance.
204
    $field = array(
205
      'translatable' => FALSE,
206
      'entity_types' => array(),
207
      'settings' => array(
208
        'handler' => 'base',
209
        'target_type' => 'user',
210
        'handler_settings' => array(
211
          'target_bundles' => array(),
212
        ),
213
      ),
214
      'field_name' => 'test_field',
215
      'type' => 'entityreference',
216
      'cardinality' => '1',
217
    );
218

    
219
    // Build a set of test data.
220
    $users = array(
221
      'anonymous' => user_load(0),
222
      'admin' => user_load(1),
223
      'non_admin' => (object) array(
224
        'name' => 'non_admin <&>',
225
        'mail' => 'non_admin@example.com',
226
        'roles' => array(),
227
        'pass' => user_password(),
228
        'status' => 1,
229
      ),
230
      'blocked' => (object) array(
231
        'name' => 'blocked <&>',
232
        'mail' => 'blocked@example.com',
233
        'roles' => array(),
234
        'pass' => user_password(),
235
        'status' => 0,
236
      ),
237
    );
238

    
239
    // The label of the anonymous user is variable_get('anonymous').
240
    $users['anonymous']->name = variable_get('anonymous', t('Anonymous'));
241

    
242
    $user_labels = array();
243
    foreach ($users as $key => $user) {
244
      if (!isset($user->uid)) {
245
        $users[$key] = $user = user_save(drupal_anonymous_user(), (array) $user);
246
      }
247
      $user_labels[$key] = check_plain($user->name);
248
    }
249

    
250
    // Test as a non-admin.
251
    $GLOBALS['user'] = $users['non_admin'];
252
    $referencable_tests = array(
253
      array(
254
        'arguments' => array(
255
          array(NULL, 'CONTAINS'),
256
        ),
257
        'result' => array(
258
          'user' => array(
259
            $users['admin']->uid => '- Restricted access -',
260
            $users['non_admin']->uid => $user_labels['non_admin'],
261
          ),
262
        ),
263
      ),
264
      array(
265
        'arguments' => array(
266
          array('non_admin', 'CONTAINS'),
267
          array('NON_ADMIN', 'CONTAINS'),
268
        ),
269
        'result' => array(
270
          'user' => array(
271
            $users['non_admin']->uid => $user_labels['non_admin'],
272
          ),
273
        ),
274
      ),
275
      array(
276
        'arguments' => array(
277
          array('invalid user', 'CONTAINS'),
278
        ),
279
        'result' => array(),
280
      ),
281
      array(
282
        'arguments' => array(
283
          array('blocked', 'CONTAINS'),
284
        ),
285
        'result' => array(),
286
      ),
287
    );
288
    $this->assertReferencable($field, $referencable_tests, 'User handler');
289

    
290
    $GLOBALS['user'] = $users['admin'];
291
    $referencable_tests = array(
292
      array(
293
        'arguments' => array(
294
          array(NULL, 'CONTAINS'),
295
        ),
296
        'result' => array(
297
          'user' => array(
298
            $users['anonymous']->uid => $user_labels['anonymous'],
299
            $users['admin']->uid => $user_labels['admin'],
300
            $users['non_admin']->uid => $user_labels['non_admin'],
301
            $users['blocked']->uid => $user_labels['blocked'],
302
          ),
303
        ),
304
      ),
305
      array(
306
        'arguments' => array(
307
          array('blocked', 'CONTAINS'),
308
        ),
309
        'result' => array(
310
          'user' => array(
311
            $users['blocked']->uid => $user_labels['blocked'],
312
          ),
313
        ),
314
      ),
315
      array(
316
        'arguments' => array(
317
          array('Anonymous', 'CONTAINS'),
318
          array('anonymous', 'CONTAINS'),
319
        ),
320
        'result' => array(
321
          'user' => array(
322
            $users['anonymous']->uid => $user_labels['anonymous'],
323
          ),
324
        ),
325
      ),
326
    );
327
    $this->assertReferencable($field, $referencable_tests, 'User handler (admin)');
328
  }
329

    
330
  /**
331
   * Test the comment-specific overrides of the entity handler.
332
   */
333
  public function testCommentHandler() {
334
    // Build a fake field instance.
335
    $field = array(
336
      'translatable' => FALSE,
337
      'entity_types' => array(),
338
      'settings' => array(
339
        'handler' => 'base',
340
        'target_type' => 'comment',
341
        'handler_settings' => array(
342
          'target_bundles' => array(),
343
        ),
344
      ),
345
      'field_name' => 'test_field',
346
      'type' => 'entityreference',
347
      'cardinality' => '1',
348
    );
349

    
350
    // Build a set of test data.
351
    $nodes = array(
352
      'published' => (object) array(
353
        'type' => 'article',
354
        'status' => 1,
355
        'title' => 'Node published',
356
        'uid' => 1,
357
      ),
358
      'unpublished' => (object) array(
359
        'type' => 'article',
360
        'status' => 0,
361
        'title' => 'Node unpublished',
362
        'uid' => 1,
363
      ),
364
    );
365
    foreach ($nodes as $node) {
366
      node_save($node);
367
    }
368

    
369
    $comments = array(
370
      'published_published' => (object) array(
371
        'nid' => $nodes['published']->nid,
372
        'uid' => 1,
373
        'cid' => NULL,
374
        'pid' => 0,
375
        'status' => COMMENT_PUBLISHED,
376
        'subject' => 'Comment Published <&>',
377
        'hostname' => ip_address(),
378
        'language' => LANGUAGE_NONE,
379
      ),
380
      'published_unpublished' => (object) array(
381
        'nid' => $nodes['published']->nid,
382
        'uid' => 1,
383
        'cid' => NULL,
384
        'pid' => 0,
385
        'status' => COMMENT_NOT_PUBLISHED,
386
        'subject' => 'Comment Unpublished <&>',
387
        'hostname' => ip_address(),
388
        'language' => LANGUAGE_NONE,
389
      ),
390
      'unpublished_published' => (object) array(
391
        'nid' => $nodes['unpublished']->nid,
392
        'uid' => 1,
393
        'cid' => NULL,
394
        'pid' => 0,
395
        'status' => COMMENT_NOT_PUBLISHED,
396
        'subject' => 'Comment Published on Unpublished node <&>',
397
        'hostname' => ip_address(),
398
        'language' => LANGUAGE_NONE,
399
      ),
400
    );
401

    
402
    $comment_labels = array();
403
    foreach ($comments as $key => $comment) {
404
      comment_save($comment);
405
      $comment_labels[$key] = check_plain($comment->subject);
406
    }
407

    
408
    // Test as a non-admin.
409
    $normal_user = $this->drupalCreateUser(array('access content', 'access comments'));
410
    $GLOBALS['user'] = $normal_user;
411
    $referencable_tests = array(
412
      array(
413
        'arguments' => array(
414
          array(NULL, 'CONTAINS'),
415
        ),
416
        'result' => array(
417
          'comment_node_article' => array(
418
            $comments['published_published']->cid => $comment_labels['published_published'],
419
          ),
420
        ),
421
      ),
422
      array(
423
        'arguments' => array(
424
          array('Published', 'CONTAINS'),
425
        ),
426
        'result' => array(
427
          'comment_node_article' => array(
428
            $comments['published_published']->cid => $comment_labels['published_published'],
429
          ),
430
        ),
431
      ),
432
      array(
433
        'arguments' => array(
434
          array('invalid comment', 'CONTAINS'),
435
        ),
436
        'result' => array(),
437
      ),
438
      array(
439
        'arguments' => array(
440
          array('Comment Unpublished', 'CONTAINS'),
441
        ),
442
        'result' => array(),
443
      ),
444
    );
445
    $this->assertReferencable($field, $referencable_tests, 'Comment handler');
446

    
447
    // Test as a comment admin.
448
    $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments'));
449
    $GLOBALS['user'] = $admin_user;
450
    $referencable_tests = array(
451
      array(
452
        'arguments' => array(
453
          array(NULL, 'CONTAINS'),
454
        ),
455
        'result' => array(
456
          'comment_node_article' => array(
457
            $comments['published_published']->cid => $comment_labels['published_published'],
458
            $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
459
          ),
460
        ),
461
      ),
462
    );
463
    $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment admin)');
464

    
465
    // Test as a node and comment admin.
466
    $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
467
    $GLOBALS['user'] = $admin_user;
468
    $referencable_tests = array(
469
      array(
470
        'arguments' => array(
471
          array(NULL, 'CONTAINS'),
472
        ),
473
        'result' => array(
474
          'comment_node_article' => array(
475
            $comments['published_published']->cid => $comment_labels['published_published'],
476
            $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
477
            $comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
478
          ),
479
        ),
480
      ),
481
    );
482
    $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment + node admin)');
483
  }
484

    
485
  /**
486
   * Assert sorting by field works for non-admins.
487
   *
488
   * Since we are sorting on a field, we need to make sure the base-table
489
   * is added, and access-control is behaving as expected.
490
   */
491
  public function testSortByField() {
492
    // Add text field to entity, to sort by.
493
    $field_info = array(
494
      'field_name' => 'field_text',
495
      'type' => 'text',
496
      'entity_types' => array('node'),
497
    );
498
    field_create_field($field_info);
499

    
500
    $instance = array(
501
      'label' => 'Text Field',
502
      'field_name' => 'field_text',
503
      'entity_type' => 'node',
504
      'bundle' => 'article',
505
      'settings' => array(),
506
      'required' => FALSE,
507
    );
508
    field_create_instance($instance);
509

    
510

    
511
    // Build a fake field instance.
512
    $field = array(
513
      'translatable' => FALSE,
514
      'entity_types' => array(),
515
      'settings' => array(
516
        'handler' => 'base',
517
        'target_type' => 'node',
518
        'handler_settings' => array(
519
          'target_bundles' => array(),
520
          // Add sorting.
521
          'sort' => array(
522
            'type' => 'field',
523
            'field' => 'field_text:value',
524
            'direction' => 'DESC',
525
          ),
526
        ),
527
      ),
528
      'field_name' => 'test_field',
529
      'type' => 'entityreference',
530
      'cardinality' => '1',
531
    );
532

    
533
    // Build a set of test data.
534
    $nodes = array(
535
      'published1' => (object) array(
536
        'type' => 'article',
537
        'status' => 1,
538
        'title' => 'Node published1 (<&>)',
539
        'uid' => 1,
540
        'field_text' => array(
541
          LANGUAGE_NONE => array(
542
            array(
543
              'value' => 1,
544
            ),
545
          ),
546
        ),
547
      ),
548
      'published2' => (object) array(
549
        'type' => 'article',
550
        'status' => 1,
551
        'title' => 'Node published2 (<&>)',
552
        'uid' => 1,
553
        'field_text' => array(
554
          LANGUAGE_NONE => array(
555
            array(
556
              'value' => 2,
557
            ),
558
          ),
559
        ),
560
      ),
561
      'unpublished' => (object) array(
562
        'type' => 'article',
563
        'status' => 0,
564
        'title' => 'Node unpublished (<&>)',
565
        'uid' => 1,
566
        'field_text' => array(
567
          LANGUAGE_NONE => array(
568
            array(
569
              'value' => 3,
570
            ),
571
          ),
572
        ),
573
      ),
574
    );
575

    
576
    $node_labels = array();
577
    foreach ($nodes as $key => $node) {
578
      node_save($node);
579
      $node_labels[$key] = check_plain($node->title);
580
    }
581

    
582
    // Test as a non-admin.
583
    $normal_user = $this->drupalCreateUser(array('access content'));
584
    $GLOBALS['user'] = $normal_user;
585

    
586
    $handler = entityreference_get_selection_handler($field);
587

    
588
    // Not only assert the result, but make sure the keys are sorted as
589
    // expected.
590
    $result = $handler->getReferencableEntities();
591
    $expected_result = array(
592
      $nodes['published2']->nid => $node_labels['published2'],
593
      $nodes['published1']->nid => $node_labels['published1'],
594
    );
595
    $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values for non-admin.');
596
  }
597
}