entityreference.handlers.test 18 KB

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