entityreference.handlers.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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'], t('Valid result set returned by @handler.', array('@handler' => $handler_name)));
  26. $result = call_user_func_array(array($handler, 'countReferencableEntities'), $arguments);
  27. $this->assertEqual($result, count($test['result']), t('Valid count returned by @handler.', array('@handler' => $handler_name)));
  28. }
  29. }
  30. }
  31. /**
  32. * Test the node-specific overrides of the entity handler.
  33. */
  34. public function testNodeHandler() {
  35. // Build a fake field instance.
  36. $field = array(
  37. 'translatable' => FALSE,
  38. 'entity_types' => array(),
  39. 'settings' => array(
  40. 'handler' => 'base',
  41. 'target_type' => 'node',
  42. 'handler_settings' => array(
  43. 'target_bundles' => array(),
  44. ),
  45. ),
  46. 'field_name' => 'test_field',
  47. 'type' => 'entityreference',
  48. 'cardinality' => '1',
  49. );
  50. // Build a set of test data.
  51. // Titles contain HTML-special characters to test escaping.
  52. $nodes = array(
  53. 'published1' => (object) array(
  54. 'type' => 'article',
  55. 'status' => 1,
  56. 'title' => 'Node published1 (<&>)',
  57. 'uid' => 1,
  58. ),
  59. 'published2' => (object) array(
  60. 'type' => 'article',
  61. 'status' => 1,
  62. 'title' => 'Node published2 (<&>)',
  63. 'uid' => 1,
  64. ),
  65. 'unpublished' => (object) array(
  66. 'type' => 'article',
  67. 'status' => 0,
  68. 'title' => 'Node unpublished (<&>)',
  69. 'uid' => 1,
  70. ),
  71. );
  72. $node_labels = array();
  73. foreach ($nodes as $key => $node) {
  74. node_save($node);
  75. $node_labels[$key] = check_plain($node->title);
  76. }
  77. // Test as a non-admin.
  78. $normal_user = $this->drupalCreateUser(array('access content'));
  79. $GLOBALS['user'] = $normal_user;
  80. $referencable_tests = array(
  81. array(
  82. 'arguments' => array(
  83. array(NULL, 'CONTAINS'),
  84. ),
  85. 'result' => array(
  86. $nodes['published1']->nid => $node_labels['published1'],
  87. $nodes['published2']->nid => $node_labels['published2'],
  88. ),
  89. ),
  90. array(
  91. 'arguments' => array(
  92. array('published1', 'CONTAINS'),
  93. array('Published1', 'CONTAINS'),
  94. ),
  95. 'result' => array(
  96. $nodes['published1']->nid => $node_labels['published1'],
  97. ),
  98. ),
  99. array(
  100. 'arguments' => array(
  101. array('published2', 'CONTAINS'),
  102. array('Published2', 'CONTAINS'),
  103. ),
  104. 'result' => array(
  105. $nodes['published2']->nid => $node_labels['published2'],
  106. ),
  107. ),
  108. array(
  109. 'arguments' => array(
  110. array('invalid node', 'CONTAINS'),
  111. ),
  112. 'result' => array(),
  113. ),
  114. array(
  115. 'arguments' => array(
  116. array('Node unpublished', 'CONTAINS'),
  117. ),
  118. 'result' => array(),
  119. ),
  120. );
  121. $this->assertReferencable($field, $referencable_tests, 'Node handler');
  122. // Test as an admin.
  123. $admin_user = $this->drupalCreateUser(array('access content', 'bypass node access'));
  124. $GLOBALS['user'] = $admin_user;
  125. $referencable_tests = array(
  126. array(
  127. 'arguments' => array(
  128. array(NULL, 'CONTAINS'),
  129. ),
  130. 'result' => array(
  131. $nodes['published1']->nid => $node_labels['published1'],
  132. $nodes['published2']->nid => $node_labels['published2'],
  133. $nodes['unpublished']->nid => $node_labels['unpublished'],
  134. ),
  135. ),
  136. array(
  137. 'arguments' => array(
  138. array('Node unpublished', 'CONTAINS'),
  139. ),
  140. 'result' => array(
  141. $nodes['unpublished']->nid => $node_labels['unpublished'],
  142. ),
  143. ),
  144. );
  145. $this->assertReferencable($field, $referencable_tests, 'Node handler (admin)');
  146. }
  147. /**
  148. * Test the user-specific overrides of the entity handler.
  149. */
  150. public function testUserHandler() {
  151. // Build a fake field instance.
  152. $field = array(
  153. 'translatable' => FALSE,
  154. 'entity_types' => array(),
  155. 'settings' => array(
  156. 'handler' => 'base',
  157. 'target_type' => 'user',
  158. 'handler_settings' => array(
  159. 'target_bundles' => array(),
  160. ),
  161. ),
  162. 'field_name' => 'test_field',
  163. 'type' => 'entityreference',
  164. 'cardinality' => '1',
  165. );
  166. // Build a set of test data.
  167. $users = array(
  168. 'anonymous' => user_load(0),
  169. 'admin' => user_load(1),
  170. 'non_admin' => (object) array(
  171. 'name' => 'non_admin <&>',
  172. 'mail' => 'non_admin@example.com',
  173. 'roles' => array(),
  174. 'pass' => user_password(),
  175. 'status' => 1,
  176. ),
  177. 'blocked' => (object) array(
  178. 'name' => 'blocked <&>',
  179. 'mail' => 'blocked@example.com',
  180. 'roles' => array(),
  181. 'pass' => user_password(),
  182. 'status' => 0,
  183. ),
  184. );
  185. // The label of the anonymous user is variable_get('anonymous').
  186. $users['anonymous']->name = variable_get('anonymous', t('Anonymous'));
  187. $user_labels = array();
  188. foreach ($users as $key => $user) {
  189. if (!isset($user->uid)) {
  190. $users[$key] = $user = user_save(drupal_anonymous_user(), (array) $user);
  191. }
  192. $user_labels[$key] = check_plain($user->name);
  193. }
  194. // Test as a non-admin.
  195. $GLOBALS['user'] = $users['non_admin'];
  196. $referencable_tests = array(
  197. array(
  198. 'arguments' => array(
  199. array(NULL, 'CONTAINS'),
  200. ),
  201. 'result' => array(
  202. $users['admin']->uid => $user_labels['admin'],
  203. $users['non_admin']->uid => $user_labels['non_admin'],
  204. ),
  205. ),
  206. array(
  207. 'arguments' => array(
  208. array('non_admin', 'CONTAINS'),
  209. array('NON_ADMIN', 'CONTAINS'),
  210. ),
  211. 'result' => array(
  212. $users['non_admin']->uid => $user_labels['non_admin'],
  213. ),
  214. ),
  215. array(
  216. 'arguments' => array(
  217. array('invalid user', 'CONTAINS'),
  218. ),
  219. 'result' => array(),
  220. ),
  221. array(
  222. 'arguments' => array(
  223. array('blocked', 'CONTAINS'),
  224. ),
  225. 'result' => array(),
  226. ),
  227. );
  228. $this->assertReferencable($field, $referencable_tests, 'User handler');
  229. $GLOBALS['user'] = $users['admin'];
  230. $referencable_tests = array(
  231. array(
  232. 'arguments' => array(
  233. array(NULL, 'CONTAINS'),
  234. ),
  235. 'result' => array(
  236. $users['anonymous']->uid => $user_labels['anonymous'],
  237. $users['admin']->uid => $user_labels['admin'],
  238. $users['non_admin']->uid => $user_labels['non_admin'],
  239. $users['blocked']->uid => $user_labels['blocked'],
  240. ),
  241. ),
  242. array(
  243. 'arguments' => array(
  244. array('blocked', 'CONTAINS'),
  245. ),
  246. 'result' => array(
  247. $users['blocked']->uid => $user_labels['blocked'],
  248. ),
  249. ),
  250. array(
  251. 'arguments' => array(
  252. array('Anonymous', 'CONTAINS'),
  253. array('anonymous', 'CONTAINS'),
  254. ),
  255. 'result' => array(
  256. $users['anonymous']->uid => $user_labels['anonymous'],
  257. ),
  258. ),
  259. );
  260. $this->assertReferencable($field, $referencable_tests, 'User handler (admin)');
  261. }
  262. /**
  263. * Test the comment-specific overrides of the entity handler.
  264. */
  265. public function testCommentHandler() {
  266. // Build a fake field instance.
  267. $field = array(
  268. 'translatable' => FALSE,
  269. 'entity_types' => array(),
  270. 'settings' => array(
  271. 'handler' => 'base',
  272. 'target_type' => 'comment',
  273. 'handler_settings' => array(
  274. 'target_bundles' => array(),
  275. ),
  276. ),
  277. 'field_name' => 'test_field',
  278. 'type' => 'entityreference',
  279. 'cardinality' => '1',
  280. );
  281. // Build a set of test data.
  282. $nodes = array(
  283. 'published' => (object) array(
  284. 'type' => 'article',
  285. 'status' => 1,
  286. 'title' => 'Node published',
  287. 'uid' => 1,
  288. ),
  289. 'unpublished' => (object) array(
  290. 'type' => 'article',
  291. 'status' => 0,
  292. 'title' => 'Node unpublished',
  293. 'uid' => 1,
  294. ),
  295. );
  296. foreach ($nodes as $node) {
  297. node_save($node);
  298. }
  299. $comments = array(
  300. 'published_published' => (object) array(
  301. 'nid' => $nodes['published']->nid,
  302. 'uid' => 1,
  303. 'cid' => NULL,
  304. 'pid' => 0,
  305. 'status' => COMMENT_PUBLISHED,
  306. 'subject' => 'Comment Published <&>',
  307. 'hostname' => ip_address(),
  308. 'language' => LANGUAGE_NONE,
  309. ),
  310. 'published_unpublished' => (object) array(
  311. 'nid' => $nodes['published']->nid,
  312. 'uid' => 1,
  313. 'cid' => NULL,
  314. 'pid' => 0,
  315. 'status' => COMMENT_NOT_PUBLISHED,
  316. 'subject' => 'Comment Unpublished <&>',
  317. 'hostname' => ip_address(),
  318. 'language' => LANGUAGE_NONE,
  319. ),
  320. 'unpublished_published' => (object) array(
  321. 'nid' => $nodes['unpublished']->nid,
  322. 'uid' => 1,
  323. 'cid' => NULL,
  324. 'pid' => 0,
  325. 'status' => COMMENT_NOT_PUBLISHED,
  326. 'subject' => 'Comment Published on Unpublished node <&>',
  327. 'hostname' => ip_address(),
  328. 'language' => LANGUAGE_NONE,
  329. ),
  330. );
  331. $comment_labels = array();
  332. foreach ($comments as $key => $comment) {
  333. comment_save($comment);
  334. $comment_labels[$key] = check_plain($comment->subject);
  335. }
  336. // Test as a non-admin.
  337. $normal_user = $this->drupalCreateUser(array('access content', 'access comments'));
  338. $GLOBALS['user'] = $normal_user;
  339. $referencable_tests = array(
  340. array(
  341. 'arguments' => array(
  342. array(NULL, 'CONTAINS'),
  343. ),
  344. 'result' => array(
  345. $comments['published_published']->cid => $comment_labels['published_published'],
  346. ),
  347. ),
  348. array(
  349. 'arguments' => array(
  350. array('Published', 'CONTAINS'),
  351. ),
  352. 'result' => array(
  353. $comments['published_published']->cid => $comment_labels['published_published'],
  354. ),
  355. ),
  356. array(
  357. 'arguments' => array(
  358. array('invalid comment', 'CONTAINS'),
  359. ),
  360. 'result' => array(),
  361. ),
  362. array(
  363. 'arguments' => array(
  364. array('Comment Unpublished', 'CONTAINS'),
  365. ),
  366. 'result' => array(),
  367. ),
  368. );
  369. $this->assertReferencable($field, $referencable_tests, 'Comment handler');
  370. // Test as a comment admin.
  371. $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments'));
  372. $GLOBALS['user'] = $admin_user;
  373. $referencable_tests = array(
  374. array(
  375. 'arguments' => array(
  376. array(NULL, 'CONTAINS'),
  377. ),
  378. 'result' => array(
  379. $comments['published_published']->cid => $comment_labels['published_published'],
  380. $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
  381. ),
  382. ),
  383. );
  384. $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment admin)');
  385. // Test as a node and comment admin.
  386. $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
  387. $GLOBALS['user'] = $admin_user;
  388. $referencable_tests = array(
  389. array(
  390. 'arguments' => array(
  391. array(NULL, 'CONTAINS'),
  392. ),
  393. 'result' => array(
  394. $comments['published_published']->cid => $comment_labels['published_published'],
  395. $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
  396. $comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
  397. ),
  398. ),
  399. );
  400. $this->assertReferencable($field, $referencable_tests, 'Comment handler (comment + node admin)');
  401. }
  402. }