entityreference.handlers.test 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. }
  186. /**
  187. * Test the user-specific overrides of the entity handler.
  188. */
  189. public function testUserHandler() {
  190. // Build a fake field instance.
  191. $field = array(
  192. 'translatable' => FALSE,
  193. 'entity_types' => array(),
  194. 'settings' => array(
  195. 'handler' => 'base',
  196. 'target_type' => 'user',
  197. 'handler_settings' => array(
  198. 'target_bundles' => array(),
  199. ),
  200. ),
  201. 'field_name' => 'test_field',
  202. 'type' => 'entityreference',
  203. 'cardinality' => '1',
  204. );
  205. // Build a set of test data.
  206. $users = array(
  207. 'anonymous' => user_load(0),
  208. 'admin' => user_load(1),
  209. 'non_admin' => (object) array(
  210. 'name' => 'non_admin <&>',
  211. 'mail' => 'non_admin@example.com',
  212. 'roles' => array(),
  213. 'pass' => user_password(),
  214. 'status' => 1,
  215. ),
  216. 'blocked' => (object) array(
  217. 'name' => 'blocked <&>',
  218. 'mail' => 'blocked@example.com',
  219. 'roles' => array(),
  220. 'pass' => user_password(),
  221. 'status' => 0,
  222. ),
  223. );
  224. // The label of the anonymous user is variable_get('anonymous').
  225. $users['anonymous']->name = variable_get('anonymous', t('Anonymous'));
  226. $user_labels = array();
  227. foreach ($users as $key => $user) {
  228. if (!isset($user->uid)) {
  229. $users[$key] = $user = user_save(drupal_anonymous_user(), (array) $user);
  230. }
  231. $user_labels[$key] = check_plain($user->name);
  232. }
  233. // Test as a non-admin.
  234. $GLOBALS['user'] = $users['non_admin'];
  235. $referencable_tests = array(
  236. array(
  237. 'arguments' => array(
  238. array(NULL, 'CONTAINS'),
  239. ),
  240. 'result' => array(
  241. 'user' => array(
  242. $users['admin']->uid => '- Restricted access -',
  243. $users['non_admin']->uid => $user_labels['non_admin'],
  244. ),
  245. ),
  246. ),
  247. array(
  248. 'arguments' => array(
  249. array('non_admin', 'CONTAINS'),
  250. array('NON_ADMIN', 'CONTAINS'),
  251. ),
  252. 'result' => array(
  253. 'user' => array(
  254. $users['non_admin']->uid => $user_labels['non_admin'],
  255. ),
  256. ),
  257. ),
  258. array(
  259. 'arguments' => array(
  260. array('invalid user', 'CONTAINS'),
  261. ),
  262. 'result' => array(),
  263. ),
  264. array(
  265. 'arguments' => array(
  266. array('blocked', 'CONTAINS'),
  267. ),
  268. 'result' => array(),
  269. ),
  270. );
  271. $this->assertReferencable($field, $referencable_tests, 'User handler');
  272. $GLOBALS['user'] = $users['admin'];
  273. $referencable_tests = array(
  274. array(
  275. 'arguments' => array(
  276. array(NULL, 'CONTAINS'),
  277. ),
  278. 'result' => array(
  279. 'user' => array(
  280. $users['anonymous']->uid => $user_labels['anonymous'],
  281. $users['admin']->uid => $user_labels['admin'],
  282. $users['non_admin']->uid => $user_labels['non_admin'],
  283. $users['blocked']->uid => $user_labels['blocked'],
  284. ),
  285. ),
  286. ),
  287. array(
  288. 'arguments' => array(
  289. array('blocked', 'CONTAINS'),
  290. ),
  291. 'result' => array(
  292. 'user' => array(
  293. $users['blocked']->uid => $user_labels['blocked'],
  294. ),
  295. ),
  296. ),
  297. array(
  298. 'arguments' => array(
  299. array('Anonymous', 'CONTAINS'),
  300. array('anonymous', 'CONTAINS'),
  301. ),
  302. 'result' => array(
  303. 'user' => array(
  304. $users['anonymous']->uid => $user_labels['anonymous'],
  305. ),
  306. ),
  307. ),
  308. );
  309. $this->assertReferencable($field, $referencable_tests, 'User handler (admin)');
  310. }
  311. /**
  312. * Test the comment-specific overrides of the entity handler.
  313. */
  314. public function testCommentHandler() {
  315. // Build a fake field instance.
  316. $field = array(
  317. 'translatable' => FALSE,
  318. 'entity_types' => array(),
  319. 'settings' => array(
  320. 'handler' => 'base',
  321. 'target_type' => 'comment',
  322. 'handler_settings' => array(
  323. 'target_bundles' => array(),
  324. ),
  325. ),
  326. 'field_name' => 'test_field',
  327. 'type' => 'entityreference',
  328. 'cardinality' => '1',
  329. );
  330. // Build a set of test data.
  331. $nodes = array(
  332. 'published' => (object) array(
  333. 'type' => 'article',
  334. 'status' => 1,
  335. 'title' => 'Node published',
  336. 'uid' => 1,
  337. ),
  338. 'unpublished' => (object) array(
  339. 'type' => 'article',
  340. 'status' => 0,
  341. 'title' => 'Node unpublished',
  342. 'uid' => 1,
  343. ),
  344. );
  345. foreach ($nodes as $node) {
  346. node_save($node);
  347. }
  348. $comments = array(
  349. 'published_published' => (object) array(
  350. 'nid' => $nodes['published']->nid,
  351. 'uid' => 1,
  352. 'cid' => NULL,
  353. 'pid' => 0,
  354. 'status' => COMMENT_PUBLISHED,
  355. 'subject' => 'Comment Published <&>',
  356. 'hostname' => ip_address(),
  357. 'language' => LANGUAGE_NONE,
  358. ),
  359. 'published_unpublished' => (object) array(
  360. 'nid' => $nodes['published']->nid,
  361. 'uid' => 1,
  362. 'cid' => NULL,
  363. 'pid' => 0,
  364. 'status' => COMMENT_NOT_PUBLISHED,
  365. 'subject' => 'Comment Unpublished <&>',
  366. 'hostname' => ip_address(),
  367. 'language' => LANGUAGE_NONE,
  368. ),
  369. 'unpublished_published' => (object) array(
  370. 'nid' => $nodes['unpublished']->nid,
  371. 'uid' => 1,
  372. 'cid' => NULL,
  373. 'pid' => 0,
  374. 'status' => COMMENT_NOT_PUBLISHED,
  375. 'subject' => 'Comment Published on Unpublished node <&>',
  376. 'hostname' => ip_address(),
  377. 'language' => LANGUAGE_NONE,
  378. ),
  379. );
  380. $comment_labels = array();
  381. foreach ($comments as $key => $comment) {
  382. comment_save($comment);
  383. $comment_labels[$key] = check_plain($comment->subject);
  384. }
  385. // Test as a non-admin.
  386. $normal_user = $this->drupalCreateUser(array('access content', 'access comments'));
  387. $GLOBALS['user'] = $normal_user;
  388. $referencable_tests = array(
  389. array(
  390. 'arguments' => array(
  391. array(NULL, 'CONTAINS'),
  392. ),
  393. 'result' => array(
  394. 'comment_node_article' => array(
  395. $comments['published_published']->cid => $comment_labels['published_published'],
  396. ),
  397. ),
  398. ),
  399. array(
  400. 'arguments' => array(
  401. array('Published', 'CONTAINS'),
  402. ),
  403. 'result' => array(
  404. 'comment_node_article' => array(
  405. $comments['published_published']->cid => $comment_labels['published_published'],
  406. ),
  407. ),
  408. ),
  409. array(
  410. 'arguments' => array(
  411. array('invalid comment', 'CONTAINS'),
  412. ),
  413. 'result' => array(),
  414. ),
  415. array(
  416. 'arguments' => array(
  417. array('Comment Unpublished', 'CONTAINS'),
  418. ),
  419. 'result' => array(),
  420. ),
  421. );
  422. $this->assertReferencable($field, $referencable_tests, 'Comment handler');
  423. // Test as a comment admin.
  424. $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments'));
  425. $GLOBALS['user'] = $admin_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. $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
  435. ),
  436. ),
  437. ),
  438. );
  439. $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment admin)');
  440. // Test as a node and comment admin.
  441. $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
  442. $GLOBALS['user'] = $admin_user;
  443. $referencable_tests = array(
  444. array(
  445. 'arguments' => array(
  446. array(NULL, 'CONTAINS'),
  447. ),
  448. 'result' => array(
  449. 'comment_node_article' => array(
  450. $comments['published_published']->cid => $comment_labels['published_published'],
  451. $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
  452. $comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
  453. ),
  454. ),
  455. ),
  456. );
  457. $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment + node admin)');
  458. }
  459. /**
  460. * Assert sorting by field works for non-admins.
  461. *
  462. * Since we are sorting on a field, we need to make sure the base-table
  463. * is added, and access-control is behaving as expected.
  464. */
  465. public function testSortByField() {
  466. // Add text field to entity, to sort by.
  467. $field_info = array(
  468. 'field_name' => 'field_text',
  469. 'type' => 'text',
  470. 'entity_types' => array('node'),
  471. );
  472. field_create_field($field_info);
  473. $instance = array(
  474. 'label' => 'Text Field',
  475. 'field_name' => 'field_text',
  476. 'entity_type' => 'node',
  477. 'bundle' => 'article',
  478. 'settings' => array(),
  479. 'required' => FALSE,
  480. );
  481. field_create_instance($instance);
  482. // Build a fake field instance.
  483. $field = array(
  484. 'translatable' => FALSE,
  485. 'entity_types' => array(),
  486. 'settings' => array(
  487. 'handler' => 'base',
  488. 'target_type' => 'node',
  489. 'handler_settings' => array(
  490. 'target_bundles' => array(),
  491. // Add sorting.
  492. 'sort' => array(
  493. 'type' => 'field',
  494. 'field' => 'field_text:value',
  495. 'direction' => 'DESC',
  496. ),
  497. ),
  498. ),
  499. 'field_name' => 'test_field',
  500. 'type' => 'entityreference',
  501. 'cardinality' => '1',
  502. );
  503. // Build a set of test data.
  504. $nodes = array(
  505. 'published1' => (object) array(
  506. 'type' => 'article',
  507. 'status' => 1,
  508. 'title' => 'Node published1 (<&>)',
  509. 'uid' => 1,
  510. 'field_text' => array(
  511. LANGUAGE_NONE => array(
  512. array(
  513. 'value' => 1,
  514. ),
  515. ),
  516. ),
  517. ),
  518. 'published2' => (object) array(
  519. 'type' => 'article',
  520. 'status' => 1,
  521. 'title' => 'Node published2 (<&>)',
  522. 'uid' => 1,
  523. 'field_text' => array(
  524. LANGUAGE_NONE => array(
  525. array(
  526. 'value' => 2,
  527. ),
  528. ),
  529. ),
  530. ),
  531. 'unpublished' => (object) array(
  532. 'type' => 'article',
  533. 'status' => 0,
  534. 'title' => 'Node unpublished (<&>)',
  535. 'uid' => 1,
  536. 'field_text' => array(
  537. LANGUAGE_NONE => array(
  538. array(
  539. 'value' => 3,
  540. ),
  541. ),
  542. ),
  543. ),
  544. );
  545. $node_labels = array();
  546. foreach ($nodes as $key => $node) {
  547. node_save($node);
  548. $node_labels[$key] = check_plain($node->title);
  549. }
  550. // Test as a non-admin.
  551. $normal_user = $this->drupalCreateUser(array('access content'));
  552. $GLOBALS['user'] = $normal_user;
  553. $handler = entityreference_get_selection_handler($field);
  554. // Not only assert the result, but make sure the keys are sorted as
  555. // expected.
  556. $result = $handler->getReferencableEntities();
  557. $expected_result = array(
  558. $nodes['published2']->nid => $node_labels['published2'],
  559. $nodes['published1']->nid => $node_labels['published1'],
  560. );
  561. $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values for non-admin.');
  562. }
  563. }