entity_query.test 66 KB

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