entity_query.test 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. <?php
  2. /**
  3. * @file
  4. * Unit test file for the entity API.
  5. */
  6. /**
  7. * Tests EntityFieldQuery.
  8. */
  9. class EntityFieldQueryTestCase extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Entity query',
  13. 'description' => 'Test the EntityFieldQuery class.',
  14. 'group' => 'Entity API',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp(array('node', 'field_test', 'entity_query_access_test', 'node_access_test'));
  19. field_test_create_bundle('bundle1');
  20. field_test_create_bundle('bundle2');
  21. field_test_create_bundle('test_bundle');
  22. field_test_create_bundle('test_entity_bundle');
  23. $instances = array();
  24. $this->fields = array();
  25. $this->field_names[0] = $field_name = drupal_strtolower($this->randomName() . '_field_name');
  26. $field = array('field_name' => $field_name, 'type' => 'test_field', 'cardinality' => 4);
  27. $field = field_create_field($field);
  28. $this->fields[0] = $field;
  29. $instance = array(
  30. 'field_name' => $field_name,
  31. 'entity_type' => '',
  32. 'bundle' => '',
  33. 'label' => $this->randomName() . '_label',
  34. 'description' => $this->randomName() . '_description',
  35. 'weight' => mt_rand(0, 127),
  36. 'settings' => array(
  37. 'test_instance_setting' => $this->randomName(),
  38. ),
  39. 'widget' => array(
  40. 'type' => 'test_field_widget',
  41. 'label' => 'Test Field',
  42. 'settings' => array(
  43. 'test_widget_setting' => $this->randomName(),
  44. )
  45. )
  46. );
  47. $instances[0] = $instance;
  48. // Add an instance to that bundle.
  49. $instances[0]['bundle'] = 'bundle1';
  50. $instances[0]['entity_type'] = 'test_entity_bundle_key';
  51. field_create_instance($instances[0]);
  52. $instances[0]['bundle'] = 'bundle2';
  53. field_create_instance($instances[0]);
  54. $instances[0]['bundle'] = $instances[0]['entity_type'] = 'test_entity_bundle';
  55. field_create_instance($instances[0]);
  56. $this->field_names[1] = $field_name = drupal_strtolower($this->randomName() . '_field_name');
  57. $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4);
  58. $field = field_create_field($field);
  59. $this->fields[1] = $field;
  60. $instance = array(
  61. 'field_name' => $field_name,
  62. 'entity_type' => '',
  63. 'bundle' => '',
  64. 'label' => $this->randomName() . '_label',
  65. 'description' => $this->randomName() . '_description',
  66. 'weight' => mt_rand(0, 127),
  67. 'settings' => array(
  68. 'test_instance_setting' => $this->randomName(),
  69. ),
  70. 'widget' => array(
  71. 'type' => 'test_field_widget',
  72. 'label' => 'Test Field',
  73. 'settings' => array(
  74. 'test_widget_setting' => $this->randomName(),
  75. )
  76. )
  77. );
  78. $instances[1] = $instance;
  79. // Add a field instance to the bundles.
  80. $instances[1]['bundle'] = 'bundle1';
  81. $instances[1]['entity_type'] = 'test_entity_bundle_key';
  82. field_create_instance($instances[1]);
  83. $instances[1]['bundle'] = $instances[1]['entity_type'] = 'test_entity_bundle';
  84. field_create_instance($instances[1]);
  85. $this->instances = $instances;
  86. // Write entity base table if there is one.
  87. $entities = array();
  88. // Create entities which have a 'bundle key' defined.
  89. for ($i = 1; $i < 7; $i++) {
  90. $entity = new stdClass();
  91. $entity->ftid = $i;
  92. $entity->fttype = ($i < 5) ? 'bundle1' : 'bundle2';
  93. $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;
  94. drupal_write_record('test_entity_bundle_key', $entity);
  95. field_attach_insert('test_entity_bundle_key', $entity);
  96. }
  97. $entity = new stdClass();
  98. $entity->ftid = 5;
  99. $entity->fttype = 'test_entity_bundle';
  100. $entity->{$this->field_names[1]}[LANGUAGE_NONE][0]['shape'] = 'square';
  101. $entity->{$this->field_names[1]}[LANGUAGE_NONE][0]['color'] = 'red';
  102. $entity->{$this->field_names[1]}[LANGUAGE_NONE][1]['shape'] = 'circle';
  103. $entity->{$this->field_names[1]}[LANGUAGE_NONE][1]['color'] = 'blue';
  104. drupal_write_record('test_entity_bundle', $entity);
  105. field_attach_insert('test_entity_bundle', $entity);
  106. $instances[2] = $instance;
  107. $instances[2]['bundle'] = 'test_bundle';
  108. $instances[2]['field_name'] = $this->field_names[0];
  109. $instances[2]['entity_type'] = 'test_entity';
  110. field_create_instance($instances[2]);
  111. // Create entities with support for revisions.
  112. for ($i = 1; $i < 5; $i++) {
  113. $entity = new stdClass();
  114. $entity->ftid = $i;
  115. $entity->ftvid = $i;
  116. $entity->fttype = 'test_bundle';
  117. $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;
  118. drupal_write_record('test_entity', $entity);
  119. field_attach_insert('test_entity', $entity);
  120. drupal_write_record('test_entity_revision', $entity);
  121. }
  122. // Add two revisions to an entity.
  123. for ($i = 100; $i < 102; $i++) {
  124. $entity = new stdClass();
  125. $entity->ftid = 4;
  126. $entity->ftvid = $i;
  127. $entity->fttype = 'test_bundle';
  128. $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;
  129. drupal_write_record('test_entity', $entity, 'ftid');
  130. drupal_write_record('test_entity_revision', $entity);
  131. db_update('test_entity')
  132. ->fields(array('ftvid' => $entity->ftvid))
  133. ->condition('ftid', $entity->ftid)
  134. ->execute();
  135. field_attach_update('test_entity', $entity);
  136. }
  137. }
  138. /**
  139. * Tests EntityFieldQuery.
  140. */
  141. function testEntityFieldQuery() {
  142. $query = new EntityFieldQuery();
  143. $query
  144. ->entityCondition('entity_type', 'test_entity_bundle')
  145. ->entityCondition('entity_id', '5');
  146. $this->assertEntityFieldQuery($query, array(
  147. array('test_entity_bundle', 5),
  148. ), t('Test query on an entity type with a generated bundle.'));
  149. // Test entity_type condition.
  150. $query = new EntityFieldQuery();
  151. $query->entityCondition('entity_type', 'test_entity_bundle_key');
  152. $this->assertEntityFieldQuery($query, array(
  153. array('test_entity_bundle_key', 1),
  154. array('test_entity_bundle_key', 2),
  155. array('test_entity_bundle_key', 3),
  156. array('test_entity_bundle_key', 4),
  157. array('test_entity_bundle_key', 5),
  158. array('test_entity_bundle_key', 6),
  159. ), t('Test entity entity_type condition.'));
  160. // Test entity_id condition.
  161. $query = new EntityFieldQuery();
  162. $query
  163. ->entityCondition('entity_type', 'test_entity_bundle_key')
  164. ->entityCondition('entity_id', '3');
  165. $this->assertEntityFieldQuery($query, array(
  166. array('test_entity_bundle_key', 3),
  167. ), t('Test entity entity_id condition.'));
  168. $query = new EntityFieldQuery();
  169. $query
  170. ->entityCondition('entity_type', 'test_entity_bundle_key')
  171. ->propertyCondition('ftid', '3');
  172. $this->assertEntityFieldQuery($query, array(
  173. array('test_entity_bundle_key', 3),
  174. ), t('Test entity entity_id condition and entity_id property condition.'));
  175. // Test bundle condition.
  176. $query = new EntityFieldQuery();
  177. $query
  178. ->entityCondition('entity_type', 'test_entity_bundle_key')
  179. ->entityCondition('bundle', 'bundle1');
  180. $this->assertEntityFieldQuery($query, array(
  181. array('test_entity_bundle_key', 1),
  182. array('test_entity_bundle_key', 2),
  183. array('test_entity_bundle_key', 3),
  184. array('test_entity_bundle_key', 4),
  185. ), t('Test entity bundle condition: bundle1.'));
  186. $query = new EntityFieldQuery();
  187. $query
  188. ->entityCondition('entity_type', 'test_entity_bundle_key')
  189. ->entityCondition('bundle', 'bundle2');
  190. $this->assertEntityFieldQuery($query, array(
  191. array('test_entity_bundle_key', 5),
  192. array('test_entity_bundle_key', 6),
  193. ), t('Test entity bundle condition: bundle2.'));
  194. $query = new EntityFieldQuery();
  195. $query
  196. ->entityCondition('entity_type', 'test_entity_bundle_key')
  197. ->propertyCondition('fttype', 'bundle2');
  198. $this->assertEntityFieldQuery($query, array(
  199. array('test_entity_bundle_key', 5),
  200. array('test_entity_bundle_key', 6),
  201. ), t('Test entity bundle condition and bundle property condition.'));
  202. // Test revision_id condition.
  203. $query = new EntityFieldQuery();
  204. $query
  205. ->entityCondition('entity_type', 'test_entity')
  206. ->entityCondition('revision_id', '3');
  207. $this->assertEntityFieldQuery($query, array(
  208. array('test_entity', 3),
  209. ), t('Test entity revision_id condition.'));
  210. $query = new EntityFieldQuery();
  211. $query
  212. ->entityCondition('entity_type', 'test_entity')
  213. ->propertyCondition('ftvid', '3');
  214. $this->assertEntityFieldQuery($query, array(
  215. array('test_entity', 3),
  216. ), t('Test entity revision_id condition and revision_id property condition.'));
  217. $query = new EntityFieldQuery();
  218. $query
  219. ->entityCondition('entity_type', 'test_entity')
  220. ->fieldCondition($this->fields[0], 'value', 100, '>=')
  221. ->age(FIELD_LOAD_REVISION);
  222. $this->assertEntityFieldQuery($query, array(
  223. array('test_entity', 100),
  224. array('test_entity', 101),
  225. ), t('Test revision age.'));
  226. // Test that fields attached to the non-revision supporting entity
  227. // 'test_entity_bundle_key' are reachable in FIELD_LOAD_REVISION.
  228. $query = new EntityFieldQuery();
  229. $query
  230. ->fieldCondition($this->fields[0], 'value', 100, '<')
  231. ->age(FIELD_LOAD_REVISION);
  232. $this->assertEntityFieldQuery($query, array(
  233. array('test_entity_bundle_key', 1),
  234. array('test_entity_bundle_key', 2),
  235. array('test_entity_bundle_key', 3),
  236. array('test_entity_bundle_key', 4),
  237. array('test_entity_bundle_key', 5),
  238. array('test_entity_bundle_key', 6),
  239. array('test_entity', 1),
  240. array('test_entity', 2),
  241. array('test_entity', 3),
  242. array('test_entity', 4),
  243. ), t('Test that fields are reachable from FIELD_LOAD_REVISION even for non-revision entities.'));
  244. // Test entity sort by entity_id.
  245. $query = new EntityFieldQuery();
  246. $query
  247. ->entityCondition('entity_type', 'test_entity_bundle_key')
  248. ->entityOrderBy('entity_id', 'ASC');
  249. $this->assertEntityFieldQuery($query, array(
  250. array('test_entity_bundle_key', 1),
  251. array('test_entity_bundle_key', 2),
  252. array('test_entity_bundle_key', 3),
  253. array('test_entity_bundle_key', 4),
  254. array('test_entity_bundle_key', 5),
  255. array('test_entity_bundle_key', 6),
  256. ), t('Test sort entity entity_id in ascending order.'), TRUE);
  257. $query = new EntityFieldQuery();
  258. $query
  259. ->entityCondition('entity_type', 'test_entity_bundle_key')
  260. ->entityOrderBy('entity_id', 'DESC');
  261. $this->assertEntityFieldQuery($query, array(
  262. array('test_entity_bundle_key', 6),
  263. array('test_entity_bundle_key', 5),
  264. array('test_entity_bundle_key', 4),
  265. array('test_entity_bundle_key', 3),
  266. array('test_entity_bundle_key', 2),
  267. array('test_entity_bundle_key', 1),
  268. ), t('Test sort entity entity_id in descending order.'), TRUE);
  269. // Test entity sort by entity_id, with a field condition.
  270. $query = new EntityFieldQuery();
  271. $query
  272. ->entityCondition('entity_type', 'test_entity_bundle_key')
  273. ->fieldCondition($this->fields[0], 'value', 0, '>')
  274. ->entityOrderBy('entity_id', 'ASC');
  275. $this->assertEntityFieldQuery($query, array(
  276. array('test_entity_bundle_key', 1),
  277. array('test_entity_bundle_key', 2),
  278. array('test_entity_bundle_key', 3),
  279. array('test_entity_bundle_key', 4),
  280. array('test_entity_bundle_key', 5),
  281. array('test_entity_bundle_key', 6),
  282. ), t('Test sort entity entity_id in ascending order, with a field condition.'), TRUE);
  283. $query = new EntityFieldQuery();
  284. $query
  285. ->entityCondition('entity_type', 'test_entity_bundle_key')
  286. ->fieldCondition($this->fields[0], 'value', 0, '>')
  287. ->propertyOrderBy('ftid', 'DESC');
  288. $this->assertEntityFieldQuery($query, array(
  289. array('test_entity_bundle_key', 6),
  290. array('test_entity_bundle_key', 5),
  291. array('test_entity_bundle_key', 4),
  292. array('test_entity_bundle_key', 3),
  293. array('test_entity_bundle_key', 2),
  294. array('test_entity_bundle_key', 1),
  295. ), t('Test sort entity entity_id property in descending order, with a field condition.'), TRUE);
  296. // Test property sort by entity id.
  297. $query = new EntityFieldQuery();
  298. $query
  299. ->entityCondition('entity_type', 'test_entity_bundle_key')
  300. ->propertyOrderBy('ftid', 'ASC');
  301. $this->assertEntityFieldQuery($query, array(
  302. array('test_entity_bundle_key', 1),
  303. array('test_entity_bundle_key', 2),
  304. array('test_entity_bundle_key', 3),
  305. array('test_entity_bundle_key', 4),
  306. array('test_entity_bundle_key', 5),
  307. array('test_entity_bundle_key', 6),
  308. ), t('Test sort entity entity_id property in ascending order.'), TRUE);
  309. $query = new EntityFieldQuery();
  310. $query
  311. ->entityCondition('entity_type', 'test_entity_bundle_key')
  312. ->propertyOrderBy('ftid', 'DESC');
  313. $this->assertEntityFieldQuery($query, array(
  314. array('test_entity_bundle_key', 6),
  315. array('test_entity_bundle_key', 5),
  316. array('test_entity_bundle_key', 4),
  317. array('test_entity_bundle_key', 3),
  318. array('test_entity_bundle_key', 2),
  319. array('test_entity_bundle_key', 1),
  320. ), t('Test sort entity entity_id property in descending order.'), TRUE);
  321. // Test property sort by entity id, with a field condition.
  322. $query = new EntityFieldQuery();
  323. $query
  324. ->entityCondition('entity_type', 'test_entity_bundle_key')
  325. ->fieldCondition($this->fields[0], 'value', 0, '>')
  326. ->propertyOrderBy('ftid', 'ASC');
  327. $this->assertEntityFieldQuery($query, array(
  328. array('test_entity_bundle_key', 1),
  329. array('test_entity_bundle_key', 2),
  330. array('test_entity_bundle_key', 3),
  331. array('test_entity_bundle_key', 4),
  332. array('test_entity_bundle_key', 5),
  333. array('test_entity_bundle_key', 6),
  334. ), t('Test sort entity entity_id property in ascending order, with a field condition.'), TRUE);
  335. $query = new EntityFieldQuery();
  336. $query
  337. ->entityCondition('entity_type', 'test_entity_bundle_key')
  338. ->fieldCondition($this->fields[0], 'value', 0, '>')
  339. ->propertyOrderBy('ftid', 'DESC');
  340. $this->assertEntityFieldQuery($query, array(
  341. array('test_entity_bundle_key', 6),
  342. array('test_entity_bundle_key', 5),
  343. array('test_entity_bundle_key', 4),
  344. array('test_entity_bundle_key', 3),
  345. array('test_entity_bundle_key', 2),
  346. array('test_entity_bundle_key', 1),
  347. ), t('Test sort entity entity_id property in descending order, with a field condition.'), TRUE);
  348. // Test entity sort by bundle.
  349. $query = new EntityFieldQuery();
  350. $query
  351. ->entityCondition('entity_type', 'test_entity_bundle_key')
  352. ->entityOrderBy('bundle', 'ASC')
  353. ->propertyOrderBy('ftid', 'DESC');
  354. $this->assertEntityFieldQuery($query, array(
  355. array('test_entity_bundle_key', 4),
  356. array('test_entity_bundle_key', 3),
  357. array('test_entity_bundle_key', 2),
  358. array('test_entity_bundle_key', 1),
  359. array('test_entity_bundle_key', 6),
  360. array('test_entity_bundle_key', 5),
  361. ), t('Test sort entity bundle in ascending order, property in descending order.'), TRUE);
  362. $query = new EntityFieldQuery();
  363. $query
  364. ->entityCondition('entity_type', 'test_entity_bundle_key')
  365. ->entityOrderBy('bundle', 'DESC')
  366. ->propertyOrderBy('ftid', 'ASC');
  367. $this->assertEntityFieldQuery($query, array(
  368. array('test_entity_bundle_key', 5),
  369. array('test_entity_bundle_key', 6),
  370. array('test_entity_bundle_key', 1),
  371. array('test_entity_bundle_key', 2),
  372. array('test_entity_bundle_key', 3),
  373. array('test_entity_bundle_key', 4),
  374. ), t('Test sort entity bundle in descending order, property in ascending order.'), TRUE);
  375. // Test entity sort by bundle, with a field condition.
  376. $query = new EntityFieldQuery();
  377. $query
  378. ->entityCondition('entity_type', 'test_entity_bundle_key')
  379. ->fieldCondition($this->fields[0], 'value', 0, '>')
  380. ->entityOrderBy('bundle', 'ASC')
  381. ->propertyOrderBy('ftid', 'DESC');
  382. $this->assertEntityFieldQuery($query, array(
  383. array('test_entity_bundle_key', 4),
  384. array('test_entity_bundle_key', 3),
  385. array('test_entity_bundle_key', 2),
  386. array('test_entity_bundle_key', 1),
  387. array('test_entity_bundle_key', 6),
  388. array('test_entity_bundle_key', 5),
  389. ), t('Test sort entity bundle in ascending order, property in descending order, with a field condition.'), TRUE);
  390. $query = new EntityFieldQuery();
  391. $query
  392. ->entityCondition('entity_type', 'test_entity_bundle_key')
  393. ->fieldCondition($this->fields[0], 'value', 0, '>')
  394. ->entityOrderBy('bundle', 'DESC')
  395. ->propertyOrderBy('ftid', 'ASC');
  396. $this->assertEntityFieldQuery($query, array(
  397. array('test_entity_bundle_key', 5),
  398. array('test_entity_bundle_key', 6),
  399. array('test_entity_bundle_key', 1),
  400. array('test_entity_bundle_key', 2),
  401. array('test_entity_bundle_key', 3),
  402. array('test_entity_bundle_key', 4),
  403. ), t('Test sort entity bundle in descending order, property in ascending order, with a field condition.'), TRUE);
  404. // Test entity sort by bundle, field.
  405. $query = new EntityFieldQuery();
  406. $query
  407. ->entityCondition('entity_type', 'test_entity_bundle_key')
  408. ->entityOrderBy('bundle', 'ASC')
  409. ->fieldOrderBy($this->fields[0], 'value', 'DESC');
  410. $this->assertEntityFieldQuery($query, array(
  411. array('test_entity_bundle_key', 4),
  412. array('test_entity_bundle_key', 3),
  413. array('test_entity_bundle_key', 2),
  414. array('test_entity_bundle_key', 1),
  415. array('test_entity_bundle_key', 6),
  416. array('test_entity_bundle_key', 5),
  417. ), t('Test sort entity bundle in ascending order, field in descending order.'), TRUE);
  418. $query = new EntityFieldQuery();
  419. $query
  420. ->entityCondition('entity_type', 'test_entity_bundle_key')
  421. ->entityOrderBy('bundle', 'DESC')
  422. ->fieldOrderBy($this->fields[0], 'value', 'ASC');
  423. $this->assertEntityFieldQuery($query, array(
  424. array('test_entity_bundle_key', 5),
  425. array('test_entity_bundle_key', 6),
  426. array('test_entity_bundle_key', 1),
  427. array('test_entity_bundle_key', 2),
  428. array('test_entity_bundle_key', 3),
  429. array('test_entity_bundle_key', 4),
  430. ), t('Test sort entity bundle in descending order, field in ascending order.'), TRUE);
  431. // Test entity sort by revision_id.
  432. $query = new EntityFieldQuery();
  433. $query
  434. ->entityCondition('entity_type', 'test_entity')
  435. ->entityOrderBy('revision_id', 'ASC');
  436. $this->assertEntityFieldQuery($query, array(
  437. array('test_entity', 1),
  438. array('test_entity', 2),
  439. array('test_entity', 3),
  440. array('test_entity', 4),
  441. ), t('Test sort entity revision_id in ascending order.'), TRUE);
  442. $query = new EntityFieldQuery();
  443. $query
  444. ->entityCondition('entity_type', 'test_entity')
  445. ->entityOrderBy('revision_id', 'DESC');
  446. $this->assertEntityFieldQuery($query, array(
  447. array('test_entity', 4),
  448. array('test_entity', 3),
  449. array('test_entity', 2),
  450. array('test_entity', 1),
  451. ), t('Test sort entity revision_id in descending order.'), TRUE);
  452. // Test entity sort by revision_id, with a field condition.
  453. $query = new EntityFieldQuery();
  454. $query
  455. ->entityCondition('entity_type', 'test_entity')
  456. ->fieldCondition($this->fields[0], 'value', 0, '>')
  457. ->entityOrderBy('revision_id', 'ASC');
  458. $this->assertEntityFieldQuery($query, array(
  459. array('test_entity', 1),
  460. array('test_entity', 2),
  461. array('test_entity', 3),
  462. array('test_entity', 4),
  463. ), t('Test sort entity revision_id in ascending order, with a field condition.'), TRUE);
  464. $query = new EntityFieldQuery();
  465. $query
  466. ->entityCondition('entity_type', 'test_entity')
  467. ->fieldCondition($this->fields[0], 'value', 0, '>')
  468. ->entityOrderBy('revision_id', 'DESC');
  469. $this->assertEntityFieldQuery($query, array(
  470. array('test_entity', 4),
  471. array('test_entity', 3),
  472. array('test_entity', 2),
  473. array('test_entity', 1),
  474. ), t('Test sort entity revision_id in descending order, with a field condition.'), TRUE);
  475. // Test property sort by revision_id.
  476. $query = new EntityFieldQuery();
  477. $query
  478. ->entityCondition('entity_type', 'test_entity')
  479. ->propertyOrderBy('ftvid', 'ASC');
  480. $this->assertEntityFieldQuery($query, array(
  481. array('test_entity', 1),
  482. array('test_entity', 2),
  483. array('test_entity', 3),
  484. array('test_entity', 4),
  485. ), t('Test sort entity revision_id property in ascending order.'), TRUE);
  486. $query = new EntityFieldQuery();
  487. $query
  488. ->entityCondition('entity_type', 'test_entity')
  489. ->propertyOrderBy('ftvid', 'DESC');
  490. $this->assertEntityFieldQuery($query, array(
  491. array('test_entity', 4),
  492. array('test_entity', 3),
  493. array('test_entity', 2),
  494. array('test_entity', 1),
  495. ), t('Test sort entity revision_id property in descending order.'), TRUE);
  496. // Test property sort by revision_id, with a field condition.
  497. $query = new EntityFieldQuery();
  498. $query
  499. ->entityCondition('entity_type', 'test_entity')
  500. ->fieldCondition($this->fields[0], 'value', 0, '>')
  501. ->propertyOrderBy('ftvid', 'ASC');
  502. $this->assertEntityFieldQuery($query, array(
  503. array('test_entity', 1),
  504. array('test_entity', 2),
  505. array('test_entity', 3),
  506. array('test_entity', 4),
  507. ), t('Test sort entity revision_id property in ascending order, with a field condition.'), TRUE);
  508. $query = new EntityFieldQuery();
  509. $query
  510. ->entityCondition('entity_type', 'test_entity')
  511. ->fieldCondition($this->fields[0], 'value', 0, '>')
  512. ->propertyOrderBy('ftvid', 'DESC');
  513. $this->assertEntityFieldQuery($query, array(
  514. array('test_entity', 4),
  515. array('test_entity', 3),
  516. array('test_entity', 2),
  517. array('test_entity', 1),
  518. ), t('Test sort entity revision_id property in descending order, with a field condition.'), TRUE);
  519. $query = new EntityFieldQuery();
  520. $query
  521. ->entityCondition('entity_type', 'test_entity_bundle_key')
  522. ->fieldOrderBy($this->fields[0], 'value', 'ASC');
  523. $this->assertEntityFieldQuery($query, array(
  524. array('test_entity_bundle_key', 1),
  525. array('test_entity_bundle_key', 2),
  526. array('test_entity_bundle_key', 3),
  527. array('test_entity_bundle_key', 4),
  528. array('test_entity_bundle_key', 5),
  529. array('test_entity_bundle_key', 6),
  530. ), t('Test sort field in ascending order without field condition.'), TRUE);
  531. $query = new EntityFieldQuery();
  532. $query
  533. ->entityCondition('entity_type', 'test_entity_bundle_key')
  534. ->fieldOrderBy($this->fields[0], 'value', 'DESC');
  535. $this->assertEntityFieldQuery($query, array(
  536. array('test_entity_bundle_key', 6),
  537. array('test_entity_bundle_key', 5),
  538. array('test_entity_bundle_key', 4),
  539. array('test_entity_bundle_key', 3),
  540. array('test_entity_bundle_key', 2),
  541. array('test_entity_bundle_key', 1),
  542. ), t('Test sort field in descending order without field condition.'), TRUE);
  543. $query = new EntityFieldQuery();
  544. $query
  545. ->entityCondition('entity_type', 'test_entity_bundle_key')
  546. ->fieldCondition($this->fields[0], 'value', 0, '>')
  547. ->fieldOrderBy($this->fields[0], 'value', 'ASC');
  548. $this->assertEntityFieldQuery($query, array(
  549. array('test_entity_bundle_key', 1),
  550. array('test_entity_bundle_key', 2),
  551. array('test_entity_bundle_key', 3),
  552. array('test_entity_bundle_key', 4),
  553. array('test_entity_bundle_key', 5),
  554. array('test_entity_bundle_key', 6),
  555. ), t('Test sort field in ascending order.'), TRUE);
  556. $query = new EntityFieldQuery();
  557. $query
  558. ->entityCondition('entity_type', 'test_entity_bundle_key')
  559. ->fieldCondition($this->fields[0], 'value', 0, '>')
  560. ->fieldOrderBy($this->fields[0], 'value', 'DESC');
  561. $this->assertEntityFieldQuery($query, array(
  562. array('test_entity_bundle_key', 6),
  563. array('test_entity_bundle_key', 5),
  564. array('test_entity_bundle_key', 4),
  565. array('test_entity_bundle_key', 3),
  566. array('test_entity_bundle_key', 2),
  567. array('test_entity_bundle_key', 1),
  568. ), t('Test sort field in descending order.'), TRUE);
  569. // Test "in" operation with entity entity_type condition and entity_id
  570. // property condition.
  571. $query = new EntityFieldQuery();
  572. $query
  573. ->entityCondition('entity_type', 'test_entity_bundle_key')
  574. ->propertyCondition('ftid', array(1, 3, 4), 'IN');
  575. $this->assertEntityFieldQuery($query, array(
  576. array('test_entity_bundle_key', 1),
  577. array('test_entity_bundle_key', 3),
  578. array('test_entity_bundle_key', 4),
  579. ), t('Test "in" operation with entity entity_type condition and entity_id property condition.'));
  580. // Test "in" operation with entity entity_type condition and entity_id
  581. // property condition. Sort in descending order by entity_id.
  582. $query = new EntityFieldQuery();
  583. $query
  584. ->entityCondition('entity_type', 'test_entity_bundle_key')
  585. ->propertyCondition('ftid', array(1, 3, 4), 'IN')
  586. ->propertyOrderBy('ftid', 'DESC');
  587. $this->assertEntityFieldQuery($query, array(
  588. array('test_entity_bundle_key', 4),
  589. array('test_entity_bundle_key', 3),
  590. array('test_entity_bundle_key', 1),
  591. ), t('Test "in" operation with entity entity_type condition and entity_id property condition. Sort entity_id in descending order.'), TRUE);
  592. // Test query count
  593. $query = new EntityFieldQuery();
  594. $query_count = $query
  595. ->entityCondition('entity_type', 'test_entity_bundle_key')
  596. ->count()
  597. ->execute();
  598. $this->assertEqual($query_count, 6, t('Test query count on entity condition.'));
  599. $query = new EntityFieldQuery();
  600. $query_count = $query
  601. ->entityCondition('entity_type', 'test_entity_bundle_key')
  602. ->propertyCondition('ftid', '1')
  603. ->count()
  604. ->execute();
  605. $this->assertEqual($query_count, 1, t('Test query count on entity and property condition.'));
  606. $query = new EntityFieldQuery();
  607. $query_count = $query
  608. ->entityCondition('entity_type', 'test_entity_bundle_key')
  609. ->propertyCondition('ftid', '4', '>')
  610. ->count()
  611. ->execute();
  612. $this->assertEqual($query_count, 2, t('Test query count on entity and property condition with operator.'));
  613. $query = new EntityFieldQuery();
  614. $query_count = $query
  615. ->entityCondition('entity_type', 'test_entity_bundle_key')
  616. ->fieldCondition($this->fields[0], 'value', 3, '=')
  617. ->count()
  618. ->execute();
  619. $this->assertEqual($query_count, 1, t('Test query count on field condition.'));
  620. // First, test without options.
  621. $query = new EntityFieldQuery();
  622. $query
  623. ->entityCondition('entity_type', 'test_entity_bundle_key')
  624. ->propertyCondition('fttype', 'und', 'CONTAINS');
  625. $this->assertEntityFieldQuery($query, array(
  626. array('test_entity_bundle_key', 1),
  627. array('test_entity_bundle_key', 2),
  628. array('test_entity_bundle_key', 3),
  629. array('test_entity_bundle_key', 4),
  630. array('test_entity_bundle_key', 5),
  631. array('test_entity_bundle_key', 6),
  632. ), t('Test the "contains" operation on a property.'));
  633. $query = new EntityFieldQuery();
  634. $query->fieldCondition($this->fields[1], 'shape', 'uar', 'CONTAINS');
  635. $this->assertEntityFieldQuery($query, array(
  636. array('test_entity_bundle', 5),
  637. ), t('Test the "contains" operation on a field.'));
  638. $query = new EntityFieldQuery();
  639. $query
  640. ->entityCondition('entity_type', 'test_entity_bundle_key')
  641. ->propertyCondition('ftid', 1, '=');
  642. $this->assertEntityFieldQuery($query, array(
  643. array('test_entity_bundle_key', 1),
  644. ), t('Test the "equal to" operation on a property.'));
  645. $query = new EntityFieldQuery();
  646. $query->fieldCondition($this->fields[0], 'value', 3, '=');
  647. $this->assertEntityFieldQuery($query, array(
  648. array('test_entity_bundle_key', 3),
  649. array('test_entity', 3),
  650. ), t('Test the "equal to" operation on a field.'));
  651. $query = new EntityFieldQuery();
  652. $query
  653. ->entityCondition('entity_type', 'test_entity_bundle_key')
  654. ->propertyCondition('ftid', 3, '<>');
  655. $this->assertEntityFieldQuery($query, array(
  656. array('test_entity_bundle_key', 1),
  657. array('test_entity_bundle_key', 2),
  658. array('test_entity_bundle_key', 4),
  659. array('test_entity_bundle_key', 5),
  660. array('test_entity_bundle_key', 6),
  661. ), t('Test the "not equal to" operation on a property.'));
  662. $query = new EntityFieldQuery();
  663. $query->fieldCondition($this->fields[0], 'value', 3, '<>');
  664. $this->assertEntityFieldQuery($query, array(
  665. array('test_entity_bundle_key', 1),
  666. array('test_entity_bundle_key', 2),
  667. array('test_entity_bundle_key', 4),
  668. array('test_entity_bundle_key', 5),
  669. array('test_entity_bundle_key', 6),
  670. array('test_entity', 1),
  671. array('test_entity', 2),
  672. array('test_entity', 4),
  673. ), t('Test the "not equal to" operation on a field.'));
  674. $query = new EntityFieldQuery();
  675. $query
  676. ->entityCondition('entity_type', 'test_entity_bundle_key')
  677. ->propertyCondition('ftid', 3, '!=');
  678. $this->assertEntityFieldQuery($query, array(
  679. array('test_entity_bundle_key', 1),
  680. array('test_entity_bundle_key', 2),
  681. array('test_entity_bundle_key', 4),
  682. array('test_entity_bundle_key', 5),
  683. array('test_entity_bundle_key', 6),
  684. ), t('Test the "not equal to" operation on a property.'));
  685. $query = new EntityFieldQuery();
  686. $query->fieldCondition($this->fields[0], 'value', 3, '!=');
  687. $this->assertEntityFieldQuery($query, array(
  688. array('test_entity_bundle_key', 1),
  689. array('test_entity_bundle_key', 2),
  690. array('test_entity_bundle_key', 4),
  691. array('test_entity_bundle_key', 5),
  692. array('test_entity_bundle_key', 6),
  693. array('test_entity', 1),
  694. array('test_entity', 2),
  695. array('test_entity', 4),
  696. ), t('Test the "not equal to" operation on a field.'));
  697. $query = new EntityFieldQuery();
  698. $query
  699. ->entityCondition('entity_type', 'test_entity_bundle_key')
  700. ->propertyCondition('ftid', 2, '<');
  701. $this->assertEntityFieldQuery($query, array(
  702. array('test_entity_bundle_key', 1),
  703. ), t('Test the "less than" operation on a property.'));
  704. $query = new EntityFieldQuery();
  705. $query->fieldCondition($this->fields[0], 'value', 2, '<');
  706. $this->assertEntityFieldQuery($query, array(
  707. array('test_entity_bundle_key', 1),
  708. array('test_entity', 1),
  709. ), t('Test the "less than" operation on a field.'));
  710. $query = new EntityFieldQuery();
  711. $query
  712. ->entityCondition('entity_type', 'test_entity_bundle_key')
  713. ->propertyCondition('ftid', 2, '<=');
  714. $this->assertEntityFieldQuery($query, array(
  715. array('test_entity_bundle_key', 1),
  716. array('test_entity_bundle_key', 2),
  717. ), t('Test the "less than or equal to" operation on a property.'));
  718. $query = new EntityFieldQuery();
  719. $query->fieldCondition($this->fields[0], 'value', 2, '<=');
  720. $this->assertEntityFieldQuery($query, array(
  721. array('test_entity_bundle_key', 1),
  722. array('test_entity_bundle_key', 2),
  723. array('test_entity', 1),
  724. array('test_entity', 2),
  725. ), t('Test the "less than or equal to" operation on a field.'));
  726. $query = new EntityFieldQuery();
  727. $query
  728. ->entityCondition('entity_type', 'test_entity_bundle_key')
  729. ->propertyCondition('ftid', 4, '>');
  730. $this->assertEntityFieldQuery($query, array(
  731. array('test_entity_bundle_key', 5),
  732. array('test_entity_bundle_key', 6),
  733. ), t('Test the "greater than" operation on a property.'));
  734. $query = new EntityFieldQuery();
  735. $query->fieldCondition($this->fields[0], 'value', 2, '>');
  736. $this->assertEntityFieldQuery($query, array(
  737. array('test_entity_bundle_key', 3),
  738. array('test_entity_bundle_key', 4),
  739. array('test_entity_bundle_key', 5),
  740. array('test_entity_bundle_key', 6),
  741. array('test_entity', 3),
  742. array('test_entity', 4),
  743. ), t('Test the "greater than" operation on a field.'));
  744. $query = new EntityFieldQuery();
  745. $query
  746. ->entityCondition('entity_type', 'test_entity_bundle_key')
  747. ->propertyCondition('ftid', 4, '>=');
  748. $this->assertEntityFieldQuery($query, array(
  749. array('test_entity_bundle_key', 4),
  750. array('test_entity_bundle_key', 5),
  751. array('test_entity_bundle_key', 6),
  752. ), t('Test the "greater than or equal to" operation on a property.'));
  753. $query = new EntityFieldQuery();
  754. $query->fieldCondition($this->fields[0], 'value', 3, '>=');
  755. $this->assertEntityFieldQuery($query, array(
  756. array('test_entity_bundle_key', 3),
  757. array('test_entity_bundle_key', 4),
  758. array('test_entity_bundle_key', 5),
  759. array('test_entity_bundle_key', 6),
  760. array('test_entity', 3),
  761. array('test_entity', 4),
  762. ), t('Test the "greater than or equal to" operation on a field.'));
  763. $query = new EntityFieldQuery();
  764. $query
  765. ->entityCondition('entity_type', 'test_entity_bundle_key')
  766. ->propertyCondition('ftid', array(3, 4), 'NOT IN');
  767. $this->assertEntityFieldQuery($query, array(
  768. array('test_entity_bundle_key', 1),
  769. array('test_entity_bundle_key', 2),
  770. array('test_entity_bundle_key', 5),
  771. array('test_entity_bundle_key', 6),
  772. ), t('Test the "not in" operation on a property.'));
  773. $query = new EntityFieldQuery();
  774. $query->fieldCondition($this->fields[0], 'value', array(3, 4, 100, 101), 'NOT IN');
  775. $this->assertEntityFieldQuery($query, array(
  776. array('test_entity_bundle_key', 1),
  777. array('test_entity_bundle_key', 2),
  778. array('test_entity_bundle_key', 5),
  779. array('test_entity_bundle_key', 6),
  780. array('test_entity', 1),
  781. array('test_entity', 2),
  782. ), t('Test the "not in" operation on a field.'));
  783. $query = new EntityFieldQuery();
  784. $query
  785. ->entityCondition('entity_type', 'test_entity_bundle_key')
  786. ->propertyCondition('ftid', array(3, 4), 'IN');
  787. $this->assertEntityFieldQuery($query, array(
  788. array('test_entity_bundle_key', 3),
  789. array('test_entity_bundle_key', 4),
  790. ), t('Test the "in" operation on a property.'));
  791. $query = new EntityFieldQuery();
  792. $query->fieldCondition($this->fields[0], 'value', array(2, 3), 'IN');
  793. $this->assertEntityFieldQuery($query, array(
  794. array('test_entity_bundle_key', 2),
  795. array('test_entity_bundle_key', 3),
  796. array('test_entity', 2),
  797. array('test_entity', 3),
  798. ), t('Test the "in" operation on a field.'));
  799. $query = new EntityFieldQuery();
  800. $query
  801. ->entityCondition('entity_type', 'test_entity_bundle_key')
  802. ->propertyCondition('ftid', array(1, 3), 'BETWEEN');
  803. $this->assertEntityFieldQuery($query, array(
  804. array('test_entity_bundle_key', 1),
  805. array('test_entity_bundle_key', 2),
  806. array('test_entity_bundle_key', 3),
  807. ), t('Test the "between" operation on a property.'));
  808. $query = new EntityFieldQuery();
  809. $query->fieldCondition($this->fields[0], 'value', array(1, 3), 'BETWEEN');
  810. $this->assertEntityFieldQuery($query, array(
  811. array('test_entity_bundle_key', 1),
  812. array('test_entity_bundle_key', 2),
  813. array('test_entity_bundle_key', 3),
  814. array('test_entity', 1),
  815. array('test_entity', 2),
  816. array('test_entity', 3),
  817. ), t('Test the "between" operation on a field.'));
  818. $query = new EntityFieldQuery();
  819. $query
  820. ->entityCondition('entity_type', 'test_entity_bundle_key')
  821. ->propertyCondition('fttype', 'bun', 'STARTS_WITH');
  822. $this->assertEntityFieldQuery($query, array(
  823. array('test_entity_bundle_key', 1),
  824. array('test_entity_bundle_key', 2),
  825. array('test_entity_bundle_key', 3),
  826. array('test_entity_bundle_key', 4),
  827. array('test_entity_bundle_key', 5),
  828. array('test_entity_bundle_key', 6),
  829. ), t('Test the "starts_with" operation on a property.'));
  830. $query = new EntityFieldQuery();
  831. $query->fieldCondition($this->fields[1], 'shape', 'squ', 'STARTS_WITH');
  832. $this->assertEntityFieldQuery($query, array(
  833. array('test_entity_bundle', 5),
  834. ), t('Test the "starts_with" operation on a field.'));
  835. $query = new EntityFieldQuery();
  836. $query->fieldCondition($this->fields[0], 'value', 3);
  837. $this->assertEntityFieldQuery($query, array(
  838. array('test_entity_bundle_key', 3),
  839. array('test_entity', 3),
  840. ), t('Test omission of an operator with a single item.'));
  841. $query = new EntityFieldQuery();
  842. $query->fieldCondition($this->fields[0], 'value', array(2, 3));
  843. $this->assertEntityFieldQuery($query, array(
  844. array('test_entity_bundle_key', 2),
  845. array('test_entity_bundle_key', 3),
  846. array('test_entity', 2),
  847. array('test_entity', 3),
  848. ), t('Test omission of an operator with multiple items.'));
  849. $query = new EntityFieldQuery();
  850. $query
  851. ->entityCondition('entity_type', 'test_entity_bundle_key')
  852. ->propertyCondition('ftid', 1, '>')
  853. ->fieldCondition($this->fields[0], 'value', 4, '<');
  854. $this->assertEntityFieldQuery($query, array(
  855. array('test_entity_bundle_key', 2),
  856. array('test_entity_bundle_key', 3),
  857. ), t('Test entity, property and field conditions.'));
  858. $query = new EntityFieldQuery();
  859. $query
  860. ->entityCondition('entity_type', 'test_entity_bundle_key')
  861. ->entityCondition('bundle', 'bundle', 'STARTS_WITH')
  862. ->propertyCondition('ftid', 4)
  863. ->fieldCondition($this->fields[0], 'value', 4);
  864. $this->assertEntityFieldQuery($query, array(
  865. array('test_entity_bundle_key', 4),
  866. ), t('Test entity condition with "starts_with" operation, and property and field conditions.'));
  867. $query = new EntityFieldQuery();
  868. $query
  869. ->entityCondition('entity_type', 'test_entity_bundle_key')
  870. ->propertyOrderBy('ftid', 'ASC')
  871. ->range(0, 2);
  872. $this->assertEntityFieldQuery($query, array(
  873. array('test_entity_bundle_key', 1),
  874. array('test_entity_bundle_key', 2),
  875. ), t('Test limit on a property.'), TRUE);
  876. $query = new EntityFieldQuery();
  877. $query
  878. ->entityCondition('entity_type', 'test_entity_bundle_key')
  879. ->fieldCondition($this->fields[0], 'value', 0, '>=')
  880. ->fieldOrderBy($this->fields[0], 'value', 'ASC')
  881. ->range(0, 2);
  882. $this->assertEntityFieldQuery($query, array(
  883. array('test_entity_bundle_key', 1),
  884. array('test_entity_bundle_key', 2),
  885. ), t('Test limit on a field.'), TRUE);
  886. $query = new EntityFieldQuery();
  887. $query
  888. ->entityCondition('entity_type', 'test_entity_bundle_key')
  889. ->propertyOrderBy('ftid', 'ASC')
  890. ->range(4, 6);
  891. $this->assertEntityFieldQuery($query, array(
  892. array('test_entity_bundle_key', 5),
  893. array('test_entity_bundle_key', 6),
  894. ), t('Test offset on a property.'), TRUE);
  895. $query = new EntityFieldQuery();
  896. $query
  897. ->entityCondition('entity_type', 'test_entity_bundle_key')
  898. ->fieldCondition($this->fields[0], 'value', 0, '>')
  899. ->fieldOrderBy($this->fields[0], 'value', 'ASC')
  900. ->range(2, 4);
  901. $this->assertEntityFieldQuery($query, array(
  902. array('test_entity_bundle_key', 3),
  903. array('test_entity_bundle_key', 4),
  904. array('test_entity_bundle_key', 5),
  905. array('test_entity_bundle_key', 6),
  906. ), t('Test offset on a field.'), TRUE);
  907. for ($i = 6; $i < 10; $i++) {
  908. $entity = new stdClass();
  909. $entity->ftid = $i;
  910. $entity->fttype = 'test_entity_bundle';
  911. $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i - 5;
  912. drupal_write_record('test_entity_bundle', $entity);
  913. field_attach_insert('test_entity_bundle', $entity);
  914. }
  915. $query = new EntityFieldQuery();
  916. $query->fieldCondition($this->fields[0], 'value', 2, '>');
  917. $this->assertEntityFieldQuery($query, array(
  918. array('test_entity_bundle_key', 3),
  919. array('test_entity_bundle_key', 4),
  920. array('test_entity_bundle_key', 5),
  921. array('test_entity_bundle_key', 6),
  922. array('test_entity', 3),
  923. array('test_entity', 4),
  924. array('test_entity_bundle', 8),
  925. array('test_entity_bundle', 9),
  926. ), t('Select a field across multiple entities.'));
  927. $query = new EntityFieldQuery();
  928. $query
  929. ->fieldCondition($this->fields[1], 'shape', 'square')
  930. ->fieldCondition($this->fields[1], 'color', 'blue');
  931. $this->assertEntityFieldQuery($query, array(
  932. array('test_entity_bundle', 5),
  933. ), t('Test without a delta group.'));
  934. $query = new EntityFieldQuery();
  935. $query
  936. ->fieldCondition($this->fields[1], 'shape', 'square', '=', 'group')
  937. ->fieldCondition($this->fields[1], 'color', 'blue', '=', 'group');
  938. $this->assertEntityFieldQuery($query, array(), t('Test with a delta group.'));
  939. // Test query on a deleted field.
  940. field_attach_delete_bundle('test_entity_bundle_key', 'bundle1');
  941. field_attach_delete_bundle('test_entity', 'test_bundle');
  942. $query = new EntityFieldQuery();
  943. $query->fieldCondition($this->fields[0], 'value', '3');
  944. $this->assertEntityFieldQuery($query, array(
  945. array('test_entity_bundle', 8),
  946. ), t('Test query on a field after deleting field from some entities.'));
  947. field_attach_delete_bundle('test_entity_bundle', 'test_entity_bundle');
  948. $query = new EntityFieldQuery();
  949. $query->fieldCondition($this->fields[0], 'value', '3');
  950. $this->assertEntityFieldQuery($query, array(), t('Test query on a field after deleting field from all entities.'));
  951. $query = new EntityFieldQuery();
  952. $query
  953. ->fieldCondition($this->fields[0], 'value', '3')
  954. ->deleted(TRUE);
  955. $this->assertEntityFieldQuery($query, array(
  956. array('test_entity_bundle_key', 3),
  957. array('test_entity_bundle', 8),
  958. array('test_entity', 3),
  959. ), t('Test query on a deleted field with deleted option set to TRUE.'));
  960. $pass = FALSE;
  961. $query = new EntityFieldQuery();
  962. try {
  963. $query->execute();
  964. }
  965. catch (EntityFieldQueryException $exception) {
  966. $pass = ($exception->getMessage() == t('For this query an entity type must be specified.'));
  967. }
  968. $this->assertTrue($pass, t("Can't query the universe."));
  969. }
  970. /**
  971. * Tests querying translatable fields.
  972. */
  973. function testEntityFieldQueryTranslatable() {
  974. // Make a test field translatable AND cardinality one.
  975. $this->fields[0]['translatable'] = TRUE;
  976. $this->fields[0]['cardinality'] = 1;
  977. field_update_field($this->fields[0]);
  978. field_test_entity_info_translatable('test_entity', TRUE);
  979. // Create more items with different languages.
  980. $entity = new stdClass();
  981. $entity->ftid = 1;
  982. $entity->ftvid = 1;
  983. $entity->fttype = 'test_bundle';
  984. // Set fields in two languages with one field value.
  985. foreach (array(LANGUAGE_NONE, 'en') as $langcode) {
  986. $entity->{$this->field_names[0]}[$langcode][0]['value'] = 1234;
  987. }
  988. field_attach_update('test_entity', $entity);
  989. // Look up number of results when querying a single entity with multilingual
  990. // field values.
  991. $query = new EntityFieldQuery();
  992. $query_count = $query
  993. ->entityCondition('entity_type', 'test_entity')
  994. ->entityCondition('bundle', 'test_bundle')
  995. ->entityCondition('entity_id', '1')
  996. ->fieldCondition($this->fields[0])
  997. ->count()
  998. ->execute();
  999. $this->assertEqual($query_count, 1, t("Count on translatable cardinality one field is correct."));
  1000. }
  1001. /**
  1002. * Tests field meta conditions.
  1003. */
  1004. function testEntityFieldQueryMetaConditions() {
  1005. // Make a test field translatable.
  1006. $this->fields[0]['translatable'] = TRUE;
  1007. field_update_field($this->fields[0]);
  1008. field_test_entity_info_translatable('test_entity', TRUE);
  1009. // Create more items with different languages.
  1010. $entity = new stdClass();
  1011. $entity->ftid = 1;
  1012. $entity->ftvid = 1;
  1013. $entity->fttype = 'test_bundle';
  1014. $j = 0;
  1015. foreach (array(LANGUAGE_NONE, 'en') as $langcode) {
  1016. for ($i = 0; $i < 4; $i++) {
  1017. $entity->{$this->field_names[0]}[$langcode][$i]['value'] = $i + $j;
  1018. }
  1019. $j += 4;
  1020. }
  1021. field_attach_update('test_entity', $entity);
  1022. // Test delta field meta condition.
  1023. $query = new EntityFieldQuery();
  1024. $query
  1025. ->entityCondition('entity_type', 'test_entity', '=')
  1026. ->fieldDeltaCondition($this->fields[0], 0, '>');
  1027. $this->assertEntityFieldQuery($query, array(
  1028. array('test_entity', 1),
  1029. ), t('Test with a delta meta condition.'));
  1030. // Test language field meta condition.
  1031. $query = new EntityFieldQuery();
  1032. $query
  1033. ->entityCondition('entity_type', 'test_entity', '=')
  1034. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>');
  1035. $this->assertEntityFieldQuery($query, array(
  1036. array('test_entity', 1),
  1037. ), t('Test with a language meta condition.'));
  1038. // Test language field meta condition.
  1039. $query = new EntityFieldQuery();
  1040. $query
  1041. ->entityCondition('entity_type', 'test_entity', '=')
  1042. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=');
  1043. $this->assertEntityFieldQuery($query, array(
  1044. array('test_entity', 1),
  1045. ), t('Test with a language meta condition.'));
  1046. // Test delta grouping.
  1047. $query = new EntityFieldQuery();
  1048. $query
  1049. ->entityCondition('entity_type', 'test_entity', '=')
  1050. ->fieldCondition($this->fields[0], 'value', 0, '=', 'group')
  1051. ->fieldDeltaCondition($this->fields[0], 1, '<', 'group');
  1052. $this->assertEntityFieldQuery($query, array(
  1053. array('test_entity', 1),
  1054. ), t('Test with a grouped delta meta condition.'));
  1055. $query = new EntityFieldQuery();
  1056. $query
  1057. ->entityCondition('entity_type', 'test_entity', '=')
  1058. ->fieldCondition($this->fields[0], 'value', 0, '=', 'group')
  1059. ->fieldDeltaCondition($this->fields[0], 1, '>=', 'group');
  1060. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta meta condition (empty result set).'));
  1061. // Test language grouping.
  1062. $query = new EntityFieldQuery();
  1063. $query
  1064. ->entityCondition('entity_type', 'test_entity', '=')
  1065. ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
  1066. ->fieldLanguageCondition($this->fields[0], 'en', '<>', NULL, 'group');
  1067. $this->assertEntityFieldQuery($query, array(
  1068. array('test_entity', 1),
  1069. ), t('Test with a grouped language meta condition.'));
  1070. // Test language grouping.
  1071. $query = new EntityFieldQuery();
  1072. $query
  1073. ->entityCondition('entity_type', 'test_entity', '=')
  1074. ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
  1075. ->fieldLanguageCondition($this->fields[0], 'en', '!=', NULL, 'group');
  1076. $this->assertEntityFieldQuery($query, array(
  1077. array('test_entity', 1),
  1078. ), t('Test with a grouped language meta condition.'));
  1079. $query = new EntityFieldQuery();
  1080. $query
  1081. ->entityCondition('entity_type', 'test_entity', '=')
  1082. ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
  1083. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>', NULL, 'group');
  1084. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped language meta condition (empty result set).'));
  1085. $query = new EntityFieldQuery();
  1086. $query
  1087. ->entityCondition('entity_type', 'test_entity', '=')
  1088. ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
  1089. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=', NULL, 'group');
  1090. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped language meta condition (empty result set).'));
  1091. // Test delta and language grouping.
  1092. $query = new EntityFieldQuery();
  1093. $query
  1094. ->entityCondition('entity_type', 'test_entity', '=')
  1095. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1096. ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
  1097. ->fieldLanguageCondition($this->fields[0], 'en', '<>', 'delta', 'language');
  1098. $this->assertEntityFieldQuery($query, array(
  1099. array('test_entity', 1),
  1100. ), t('Test with a grouped delta + language meta condition.'));
  1101. // Test delta and language grouping.
  1102. $query = new EntityFieldQuery();
  1103. $query
  1104. ->entityCondition('entity_type', 'test_entity', '=')
  1105. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1106. ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
  1107. ->fieldLanguageCondition($this->fields[0], 'en', '!=', 'delta', 'language');
  1108. $this->assertEntityFieldQuery($query, array(
  1109. array('test_entity', 1),
  1110. ), t('Test with a grouped delta + language meta condition.'));
  1111. $query = new EntityFieldQuery();
  1112. $query
  1113. ->entityCondition('entity_type', 'test_entity', '=')
  1114. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1115. ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
  1116. ->fieldLanguageCondition($this->fields[0], 'en', '<>', 'delta', 'language');
  1117. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, delta condition unsatisifed).'));
  1118. $query = new EntityFieldQuery();
  1119. $query
  1120. ->entityCondition('entity_type', 'test_entity', '=')
  1121. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1122. ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
  1123. ->fieldLanguageCondition($this->fields[0], 'en', '!=', 'delta', 'language');
  1124. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, delta condition unsatisifed).'));
  1125. $query = new EntityFieldQuery();
  1126. $query
  1127. ->entityCondition('entity_type', 'test_entity', '=')
  1128. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1129. ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
  1130. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>', 'delta', 'language');
  1131. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, language condition unsatisifed).'));
  1132. $query = new EntityFieldQuery();
  1133. $query
  1134. ->entityCondition('entity_type', 'test_entity', '=')
  1135. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1136. ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
  1137. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=', 'delta', 'language');
  1138. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, language condition unsatisifed).'));
  1139. $query = new EntityFieldQuery();
  1140. $query
  1141. ->entityCondition('entity_type', 'test_entity', '=')
  1142. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1143. ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
  1144. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>', 'delta', 'language');
  1145. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, both conditions unsatisifed).'));
  1146. $query = new EntityFieldQuery();
  1147. $query
  1148. ->entityCondition('entity_type', 'test_entity', '=')
  1149. ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
  1150. ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
  1151. ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=', 'delta', 'language');
  1152. $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, both conditions unsatisifed).'));
  1153. // Test grouping with another field to ensure that grouping cache is reset
  1154. // properly.
  1155. $query = new EntityFieldQuery();
  1156. $query
  1157. ->entityCondition('entity_type', 'test_entity_bundle', '=')
  1158. ->fieldCondition($this->fields[1], 'shape', 'circle', '=', 'delta', 'language')
  1159. ->fieldCondition($this->fields[1], 'color', 'blue', '=', 'delta', 'language')
  1160. ->fieldDeltaCondition($this->fields[1], 1, '=', 'delta', 'language')
  1161. ->fieldLanguageCondition($this->fields[1], LANGUAGE_NONE, '=', 'delta', 'language');
  1162. $this->assertEntityFieldQuery($query, array(
  1163. array('test_entity_bundle', 5),
  1164. ), t('Test grouping cache.'));
  1165. }
  1166. /**
  1167. * Tests the routing feature of EntityFieldQuery.
  1168. */
  1169. function testEntityFieldQueryRouting() {
  1170. // Entity-only query.
  1171. $query = new EntityFieldQuery();
  1172. $query->entityCondition('entity_type', 'test_entity_bundle_key');
  1173. $this->assertIdentical($query->queryCallback(), array($query, 'propertyQuery'), t('Entity-only queries are handled by the propertyQuery handler.'));
  1174. // Field-only query.
  1175. $query = new EntityFieldQuery();
  1176. $query->fieldCondition($this->fields[0], 'value', '3');
  1177. $this->assertIdentical($query->queryCallback(), 'field_sql_storage_field_storage_query', t('Pure field queries are handled by the Field storage handler.'));
  1178. // Mixed entity and field query.
  1179. $query = new EntityFieldQuery();
  1180. $query
  1181. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1182. ->fieldCondition($this->fields[0], 'value', '3');
  1183. $this->assertIdentical($query->queryCallback(), 'field_sql_storage_field_storage_query', t('Mixed queries are handled by the Field storage handler.'));
  1184. // Overriding with $query->executeCallback.
  1185. $query = new EntityFieldQuery();
  1186. $query->entityCondition('entity_type', 'test_entity_bundle_key');
  1187. $query->executeCallback = 'field_test_dummy_field_storage_query';
  1188. $this->assertEntityFieldQuery($query, array(
  1189. array('user', 1),
  1190. ), t('executeCallback can override the query handler.'));
  1191. // Overriding with $query->executeCallback via hook_entity_query_alter().
  1192. $query = new EntityFieldQuery();
  1193. $query->entityCondition('entity_type', 'test_entity_bundle_key');
  1194. // Add a flag that will be caught by field_test_entity_query_alter().
  1195. $query->alterMyExecuteCallbackPlease = TRUE;
  1196. $this->assertEntityFieldQuery($query, array(
  1197. array('user', 1),
  1198. ), t('executeCallback can override the query handler when set in a hook_entity_query_alter().'));
  1199. // Mixed-storage queries.
  1200. $query = new EntityFieldQuery();
  1201. $query
  1202. ->fieldCondition($this->fields[0], 'value', '3')
  1203. ->fieldCondition($this->fields[1], 'shape', 'squ', 'STARTS_WITH');
  1204. // Alter the storage of the field.
  1205. $query->fields[1]['storage']['module'] = 'dummy_storage';
  1206. try {
  1207. $query->queryCallback();
  1208. }
  1209. catch (EntityFieldQueryException $exception) {
  1210. $pass = ($exception->getMessage() == t("Can't handle more than one field storage engine"));
  1211. }
  1212. $this->assertTrue($pass, t('Cannot query across field storage engines.'));
  1213. }
  1214. /**
  1215. * Tests the pager integration of EntityFieldQuery.
  1216. */
  1217. function testEntityFieldQueryPager() {
  1218. // Test pager in propertyQuery
  1219. $_GET['page'] = '0,1';
  1220. $query = new EntityFieldQuery();
  1221. $query
  1222. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1223. ->propertyOrderBy('ftid', 'ASC')
  1224. ->pager(3, 0);
  1225. $this->assertEntityFieldQuery($query, array(
  1226. array('test_entity_bundle_key', 1),
  1227. array('test_entity_bundle_key', 2),
  1228. array('test_entity_bundle_key', 3),
  1229. ), t('Test pager integration in propertyQuery: page 1.'), TRUE);
  1230. $query = new EntityFieldQuery();
  1231. $query
  1232. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1233. ->propertyOrderBy('ftid', 'ASC')
  1234. ->pager(3, 1);
  1235. $this->assertEntityFieldQuery($query, array(
  1236. array('test_entity_bundle_key', 4),
  1237. array('test_entity_bundle_key', 5),
  1238. array('test_entity_bundle_key', 6),
  1239. ), t('Test pager integration in propertyQuery: page 2.'), TRUE);
  1240. // Test pager in field storage
  1241. $_GET['page'] = '0,1';
  1242. $query = new EntityFieldQuery();
  1243. $query
  1244. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1245. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1246. ->propertyOrderBy('ftid', 'ASC')
  1247. ->pager(2, 0);
  1248. $this->assertEntityFieldQuery($query, array(
  1249. array('test_entity_bundle_key', 1),
  1250. array('test_entity_bundle_key', 2),
  1251. ), t('Test pager integration in field storage: page 1.'), TRUE);
  1252. $query = new EntityFieldQuery();
  1253. $query
  1254. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1255. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1256. ->propertyOrderBy('ftid', 'ASC')
  1257. ->pager(2, 1);
  1258. $this->assertEntityFieldQuery($query, array(
  1259. array('test_entity_bundle_key', 3),
  1260. array('test_entity_bundle_key', 4),
  1261. ), t('Test pager integration in field storage: page 2.'), TRUE);
  1262. unset($_GET['page']);
  1263. }
  1264. /**
  1265. * Tests disabling the pager in EntityFieldQuery.
  1266. */
  1267. function testEntityFieldQueryDisablePager() {
  1268. // Test enabling a pager and then disabling it.
  1269. $query = new EntityFieldQuery();
  1270. $query
  1271. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1272. ->propertyOrderBy('ftid', 'ASC')
  1273. ->pager(1)
  1274. ->pager(0);
  1275. $this->assertEntityFieldQuery($query, array(
  1276. array('test_entity_bundle_key', 1),
  1277. array('test_entity_bundle_key', 2),
  1278. array('test_entity_bundle_key', 3),
  1279. array('test_entity_bundle_key', 4),
  1280. array('test_entity_bundle_key', 5),
  1281. array('test_entity_bundle_key', 6),
  1282. ), 'All test entities are listed when the pager is enabled and then disabled.', TRUE);
  1283. }
  1284. /**
  1285. * Tests the TableSort integration of EntityFieldQuery.
  1286. */
  1287. function testEntityFieldQueryTableSort() {
  1288. // Test TableSort in propertyQuery
  1289. $_GET['sort'] = 'asc';
  1290. $_GET['order'] = 'Id';
  1291. $header = array(
  1292. 'id' => array('data' => 'Id', 'type' => 'property', 'specifier' => 'ftid'),
  1293. 'type' => array('data' => 'Type', 'type' => 'entity', 'specifier' => 'bundle'),
  1294. );
  1295. $query = new EntityFieldQuery();
  1296. $query
  1297. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1298. ->tableSort($header);
  1299. $this->assertEntityFieldQuery($query, array(
  1300. array('test_entity_bundle_key', 1),
  1301. array('test_entity_bundle_key', 2),
  1302. array('test_entity_bundle_key', 3),
  1303. array('test_entity_bundle_key', 4),
  1304. array('test_entity_bundle_key', 5),
  1305. array('test_entity_bundle_key', 6),
  1306. ), t('Test TableSort by property: ftid ASC in propertyQuery.'), TRUE);
  1307. $_GET['sort'] = 'desc';
  1308. $_GET['order'] = 'Id';
  1309. $query = new EntityFieldQuery();
  1310. $query
  1311. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1312. ->tableSort($header);
  1313. $this->assertEntityFieldQuery($query, array(
  1314. array('test_entity_bundle_key', 6),
  1315. array('test_entity_bundle_key', 5),
  1316. array('test_entity_bundle_key', 4),
  1317. array('test_entity_bundle_key', 3),
  1318. array('test_entity_bundle_key', 2),
  1319. array('test_entity_bundle_key', 1),
  1320. ), t('Test TableSort by property: ftid DESC in propertyQuery.'), TRUE);
  1321. $_GET['sort'] = 'asc';
  1322. $_GET['order'] = 'Type';
  1323. $query = new EntityFieldQuery();
  1324. $query
  1325. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1326. ->tableSort($header);
  1327. $this->assertEntityFieldQuery($query, array(
  1328. array('test_entity_bundle_key', 1),
  1329. array('test_entity_bundle_key', 2),
  1330. array('test_entity_bundle_key', 3),
  1331. array('test_entity_bundle_key', 4),
  1332. array('test_entity_bundle_key', 5),
  1333. array('test_entity_bundle_key', 6),
  1334. ), t('Test TableSort by entity: bundle ASC in propertyQuery.'), TRUE);
  1335. $_GET['sort'] = 'desc';
  1336. $_GET['order'] = 'Type';
  1337. $query = new EntityFieldQuery();
  1338. $query
  1339. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1340. ->tableSort($header);
  1341. $this->assertEntityFieldQuery($query, array(
  1342. array('test_entity_bundle_key', 5),
  1343. array('test_entity_bundle_key', 6),
  1344. array('test_entity_bundle_key', 1),
  1345. array('test_entity_bundle_key', 2),
  1346. array('test_entity_bundle_key', 3),
  1347. array('test_entity_bundle_key', 4),
  1348. ), t('Test TableSort by entity: bundle DESC in propertyQuery.'), TRUE);
  1349. // Test TableSort in field storage
  1350. $_GET['sort'] = 'asc';
  1351. $_GET['order'] = 'Id';
  1352. $header = array(
  1353. 'id' => array('data' => 'Id', 'type' => 'property', 'specifier' => 'ftid'),
  1354. 'type' => array('data' => 'Type', 'type' => 'entity', 'specifier' => 'bundle'),
  1355. 'field' => array('data' => 'Field', 'type' => 'field', 'specifier' => array('field' => $this->field_names[0], 'column' => 'value')),
  1356. );
  1357. $query = new EntityFieldQuery();
  1358. $query
  1359. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1360. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1361. ->tableSort($header);
  1362. $this->assertEntityFieldQuery($query, array(
  1363. array('test_entity_bundle_key', 1),
  1364. array('test_entity_bundle_key', 2),
  1365. array('test_entity_bundle_key', 3),
  1366. array('test_entity_bundle_key', 4),
  1367. array('test_entity_bundle_key', 5),
  1368. array('test_entity_bundle_key', 6),
  1369. ), t('Test TableSort by property: ftid ASC in field storage.'), TRUE);
  1370. $_GET['sort'] = 'desc';
  1371. $_GET['order'] = 'Id';
  1372. $query = new EntityFieldQuery();
  1373. $query
  1374. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1375. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1376. ->tableSort($header);
  1377. $this->assertEntityFieldQuery($query, array(
  1378. array('test_entity_bundle_key', 6),
  1379. array('test_entity_bundle_key', 5),
  1380. array('test_entity_bundle_key', 4),
  1381. array('test_entity_bundle_key', 3),
  1382. array('test_entity_bundle_key', 2),
  1383. array('test_entity_bundle_key', 1),
  1384. ), t('Test TableSort by property: ftid DESC in field storage.'), TRUE);
  1385. $_GET['sort'] = 'asc';
  1386. $_GET['order'] = 'Type';
  1387. $query = new EntityFieldQuery();
  1388. $query
  1389. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1390. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1391. ->tableSort($header)
  1392. ->entityOrderBy('entity_id', 'DESC');
  1393. $this->assertEntityFieldQuery($query, array(
  1394. array('test_entity_bundle_key', 4),
  1395. array('test_entity_bundle_key', 3),
  1396. array('test_entity_bundle_key', 2),
  1397. array('test_entity_bundle_key', 1),
  1398. array('test_entity_bundle_key', 6),
  1399. array('test_entity_bundle_key', 5),
  1400. ), t('Test TableSort by entity: bundle ASC in field storage.'), TRUE);
  1401. $_GET['sort'] = 'desc';
  1402. $_GET['order'] = 'Type';
  1403. $query = new EntityFieldQuery();
  1404. $query
  1405. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1406. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1407. ->tableSort($header)
  1408. ->entityOrderBy('entity_id', 'ASC');
  1409. $this->assertEntityFieldQuery($query, array(
  1410. array('test_entity_bundle_key', 5),
  1411. array('test_entity_bundle_key', 6),
  1412. array('test_entity_bundle_key', 1),
  1413. array('test_entity_bundle_key', 2),
  1414. array('test_entity_bundle_key', 3),
  1415. array('test_entity_bundle_key', 4),
  1416. ), t('Test TableSort by entity: bundle DESC in field storage.'), TRUE);
  1417. $_GET['sort'] = 'asc';
  1418. $_GET['order'] = 'Field';
  1419. $query = new EntityFieldQuery();
  1420. $query
  1421. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1422. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1423. ->tableSort($header);
  1424. $this->assertEntityFieldQuery($query, array(
  1425. array('test_entity_bundle_key', 1),
  1426. array('test_entity_bundle_key', 2),
  1427. array('test_entity_bundle_key', 3),
  1428. array('test_entity_bundle_key', 4),
  1429. array('test_entity_bundle_key', 5),
  1430. array('test_entity_bundle_key', 6),
  1431. ), t('Test TableSort by field ASC.'), TRUE);
  1432. $_GET['sort'] = 'desc';
  1433. $_GET['order'] = 'Field';
  1434. $query = new EntityFieldQuery();
  1435. $query
  1436. ->entityCondition('entity_type', 'test_entity_bundle_key')
  1437. ->fieldCondition($this->fields[0], 'value', 0, '>')
  1438. ->tableSort($header);
  1439. $this->assertEntityFieldQuery($query, array(
  1440. array('test_entity_bundle_key', 6),
  1441. array('test_entity_bundle_key', 5),
  1442. array('test_entity_bundle_key', 4),
  1443. array('test_entity_bundle_key', 3),
  1444. array('test_entity_bundle_key', 2),
  1445. array('test_entity_bundle_key', 1),
  1446. ), t('Test TableSort by field DESC.'), TRUE);
  1447. unset($_GET['sort']);
  1448. unset($_GET['order']);
  1449. }
  1450. /**
  1451. * Tests EntityFieldQuery access on non-node entities.
  1452. */
  1453. function testEntityFieldQueryAccess() {
  1454. // Test as a user with ability to bypass node access.
  1455. $privileged_user = $this->drupalCreateUser(array('bypass node access', 'access content'));
  1456. $this->drupalLogin($privileged_user);
  1457. $this->drupalGet('entity-query-access/test/' . $this->fields[0]['field_name']);
  1458. $this->assertText('Found entity', 'Returned access response with entities.');
  1459. $this->drupalLogout();
  1460. // Test as a user that does not have ability to bypass node access or view
  1461. // all nodes.
  1462. $regular_user = $this->drupalCreateUser(array('access content'));
  1463. $this->drupalLogin($regular_user);
  1464. $this->drupalGet('entity-query-access/test/' . $this->fields[0]['field_name']);
  1465. $this->assertText('Found entity', 'Returned access response with entities.');
  1466. $this->drupalLogout();
  1467. }
  1468. /**
  1469. * Fetches the results of an EntityFieldQuery and compares.
  1470. *
  1471. * @param $query
  1472. * An EntityFieldQuery to run.
  1473. * @param $intended_results
  1474. * A list of results, every entry is again a list, first being the entity
  1475. * type, the second being the entity_id.
  1476. * @param $message
  1477. * The message to be displayed as the result of this test.
  1478. * @param $ordered
  1479. * If FALSE then the result of EntityFieldQuery will match
  1480. * $intended_results even if the order is not the same. If TRUE then order
  1481. * should match too.
  1482. */
  1483. function assertEntityFieldQuery($query, $intended_results, $message, $ordered = FALSE) {
  1484. $results = array();
  1485. try {
  1486. foreach ($query->execute() as $entity_type => $entity_ids) {
  1487. foreach ($entity_ids as $entity_id => $stub_entity) {
  1488. $results[] = array($entity_type, $entity_id);
  1489. }
  1490. }
  1491. if (!isset($ordered) || !$ordered) {
  1492. sort($results);
  1493. sort($intended_results);
  1494. }
  1495. $this->assertEqual($results, $intended_results, $message);
  1496. }
  1497. catch (Exception $e) {
  1498. $this->fail('Exception thrown: '. $e->getMessage());
  1499. }
  1500. }
  1501. /**
  1502. * Tests EFQ table prefixing with multiple conditions and an altered join.
  1503. *
  1504. * @see field_test_query_efq_table_prefixing_test_alter()
  1505. */
  1506. function testTablePrefixing() {
  1507. $query = new EntityFieldQuery();
  1508. $query = $query
  1509. ->entityCondition('entity_type', 'test_entity')
  1510. ->entityCondition('bundle', 'test_bundle')
  1511. ->entityCondition('entity_id', '1')
  1512. ->addTag('efq_table_prefixing_test');
  1513. $expected = array(array('test_entity', 1));
  1514. $this->assertEntityFieldQuery($query, $expected, 'An EntityFieldQuery returns the expected results when altered with an additional join on the base table.');
  1515. }
  1516. }