field_sql_storage.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. /**
  3. * @file
  4. * Tests for field_sql_storage.module.
  5. *
  6. * Field_sql_storage.module implements the default back-end storage plugin
  7. * for the Field Strage API.
  8. */
  9. /**
  10. * Tests field storage.
  11. */
  12. class FieldSqlStorageTestCase extends DrupalWebTestCase {
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Field SQL storage tests',
  16. 'description' => "Test field SQL storage module.",
  17. 'group' => 'Field API'
  18. );
  19. }
  20. function setUp() {
  21. parent::setUp('field_sql_storage', 'field', 'field_test', 'text');
  22. $this->field_name = strtolower($this->randomName());
  23. $this->field = array('field_name' => $this->field_name, 'type' => 'test_field', 'cardinality' => 4);
  24. $this->field = field_create_field($this->field);
  25. $this->instance = array(
  26. 'field_name' => $this->field_name,
  27. 'entity_type' => 'test_entity',
  28. 'bundle' => 'test_bundle'
  29. );
  30. $this->instance = field_create_instance($this->instance);
  31. $this->table = _field_sql_storage_tablename($this->field);
  32. $this->revision_table = _field_sql_storage_revision_tablename($this->field);
  33. }
  34. /**
  35. * Uses the mysql tables and records to verify
  36. * field_load_revision works correctly.
  37. */
  38. function testFieldAttachLoad() {
  39. $entity_type = 'test_entity';
  40. $eid = 0;
  41. $langcode = LANGUAGE_NONE;
  42. $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $this->field_name . '_value');
  43. // Insert data for four revisions to the field revisions table
  44. $query = db_insert($this->revision_table)->fields($columns);
  45. for ($evid = 0; $evid < 4; ++$evid) {
  46. $values[$evid] = array();
  47. // Note: we insert one extra value ('<=' instead of '<').
  48. for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
  49. $value = mt_rand(1, 127);
  50. $values[$evid][] = $value;
  51. $query->values(array($entity_type, $eid, $evid, $delta, $langcode, $value));
  52. }
  53. }
  54. $query->execute();
  55. // Insert data for the "most current revision" into the field table
  56. $query = db_insert($this->table)->fields($columns);
  57. foreach ($values[0] as $delta => $value) {
  58. $query->values(array($entity_type, $eid, 0, $delta, $langcode, $value));
  59. }
  60. $query->execute();
  61. // Load the "most current revision"
  62. $entity = field_test_create_stub_entity($eid, 0, $this->instance['bundle']);
  63. field_attach_load($entity_type, array($eid => $entity));
  64. foreach ($values[0] as $delta => $value) {
  65. if ($delta < $this->field['cardinality']) {
  66. $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $value, "Value $delta is loaded correctly for current revision");
  67. }
  68. else {
  69. $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}[$langcode]), "No extraneous value gets loaded for current revision.");
  70. }
  71. }
  72. // Load every revision
  73. for ($evid = 0; $evid < 4; ++$evid) {
  74. $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
  75. field_attach_load_revision($entity_type, array($eid => $entity));
  76. foreach ($values[$evid] as $delta => $value) {
  77. if ($delta < $this->field['cardinality']) {
  78. $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $value, "Value $delta for revision $evid is loaded correctly");
  79. }
  80. else {
  81. $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}[$langcode]), "No extraneous value gets loaded for revision $evid.");
  82. }
  83. }
  84. }
  85. // Add a translation in an unavailable language and verify it is not loaded.
  86. $eid = $evid = 1;
  87. $unavailable_language = 'xx';
  88. $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
  89. $values = array($entity_type, $eid, $evid, 0, $unavailable_language, mt_rand(1, 127));
  90. db_insert($this->table)->fields($columns)->values($values)->execute();
  91. db_insert($this->revision_table)->fields($columns)->values($values)->execute();
  92. field_attach_load($entity_type, array($eid => $entity));
  93. $this->assertFalse(array_key_exists($unavailable_language, $entity->{$this->field_name}), 'Field translation in an unavailable language ignored');
  94. }
  95. /**
  96. * Reads mysql to verify correct data is
  97. * written when using insert and update.
  98. */
  99. function testFieldAttachInsertAndUpdate() {
  100. $entity_type = 'test_entity';
  101. $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
  102. $langcode = LANGUAGE_NONE;
  103. // Test insert.
  104. $values = array();
  105. // Note: we try to insert one extra value ('<=' instead of '<').
  106. // TODO : test empty values filtering and "compression" (store consecutive deltas).
  107. for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
  108. $values[$delta]['value'] = mt_rand(1, 127);
  109. }
  110. $entity->{$this->field_name}[$langcode] = $rev_values[0] = $values;
  111. field_attach_insert($entity_type, $entity);
  112. $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  113. foreach ($values as $delta => $value) {
  114. if ($delta < $this->field['cardinality']) {
  115. $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], format_string("Value %delta is inserted correctly", array('%delta' => $delta)));
  116. }
  117. else {
  118. $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets inserted.");
  119. }
  120. }
  121. // Test update.
  122. $entity = field_test_create_stub_entity(0, 1, $this->instance['bundle']);
  123. $values = array();
  124. // Note: we try to update one extra value ('<=' instead of '<').
  125. for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
  126. $values[$delta]['value'] = mt_rand(1, 127);
  127. }
  128. $entity->{$this->field_name}[$langcode] = $rev_values[1] = $values;
  129. field_attach_update($entity_type, $entity);
  130. $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  131. foreach ($values as $delta => $value) {
  132. if ($delta < $this->field['cardinality']) {
  133. $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], format_string("Value %delta is updated correctly", array('%delta' => $delta)));
  134. }
  135. else {
  136. $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets updated.");
  137. }
  138. }
  139. // Check that data for both revisions are in the revision table.
  140. // We make sure each value is stored correctly, then unset it.
  141. // When an entire revision's values are unset (remembering that we
  142. // put one extra value in $values per revision), unset the entire
  143. // revision. Then, if $rev_values is empty at the end, all
  144. // revision data was found.
  145. $results = db_select($this->revision_table, 't')->fields('t')->execute();
  146. foreach ($results as $row) {
  147. $this->assertEqual($row->{$this->field_name . '_value'}, $rev_values[$row->revision_id][$row->delta]['value'], "Value {$row->delta} for revision {$row->revision_id} stored correctly");
  148. unset($rev_values[$row->revision_id][$row->delta]);
  149. if (count($rev_values[$row->revision_id]) == 1) {
  150. unset($rev_values[$row->revision_id]);
  151. }
  152. }
  153. $this->assertTrue(empty($rev_values), "All values for all revisions are stored in revision table {$this->revision_table}");
  154. // Check that update leaves the field data untouched if
  155. // $entity->{$field_name} is absent.
  156. unset($entity->{$this->field_name});
  157. field_attach_update($entity_type, $entity);
  158. $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  159. foreach ($values as $delta => $value) {
  160. if ($delta < $this->field['cardinality']) {
  161. $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], format_string("Update with no field_name entry leaves value %delta untouched", array('%delta' => $delta)));
  162. }
  163. }
  164. // Check that update with an empty $entity->$field_name empties the field.
  165. $entity->{$this->field_name} = NULL;
  166. field_attach_update($entity_type, $entity);
  167. $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  168. $this->assertEqual(count($rows), 0, "Update with an empty field_name entry empties the field.");
  169. }
  170. /**
  171. * Tests insert and update with missing or NULL fields.
  172. */
  173. function testFieldAttachSaveMissingData() {
  174. $entity_type = 'test_entity';
  175. $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
  176. $langcode = LANGUAGE_NONE;
  177. // Insert: Field is missing
  178. field_attach_insert($entity_type, $entity);
  179. $count = db_select($this->table)
  180. ->countQuery()
  181. ->execute()
  182. ->fetchField();
  183. $this->assertEqual($count, 0, 'Missing field results in no inserts');
  184. // Insert: Field is NULL
  185. $entity->{$this->field_name} = NULL;
  186. field_attach_insert($entity_type, $entity);
  187. $count = db_select($this->table)
  188. ->countQuery()
  189. ->execute()
  190. ->fetchField();
  191. $this->assertEqual($count, 0, 'NULL field results in no inserts');
  192. // Add some real data
  193. $entity->{$this->field_name}[$langcode] = array(0 => array('value' => 1));
  194. field_attach_insert($entity_type, $entity);
  195. $count = db_select($this->table)
  196. ->countQuery()
  197. ->execute()
  198. ->fetchField();
  199. $this->assertEqual($count, 1, 'Field data saved');
  200. // Update: Field is missing. Data should survive.
  201. unset($entity->{$this->field_name});
  202. field_attach_update($entity_type, $entity);
  203. $count = db_select($this->table)
  204. ->countQuery()
  205. ->execute()
  206. ->fetchField();
  207. $this->assertEqual($count, 1, 'Missing field leaves data in table');
  208. // Update: Field is NULL. Data should be wiped.
  209. $entity->{$this->field_name} = NULL;
  210. field_attach_update($entity_type, $entity);
  211. $count = db_select($this->table)
  212. ->countQuery()
  213. ->execute()
  214. ->fetchField();
  215. $this->assertEqual($count, 0, 'NULL field leaves no data in table');
  216. // Add a translation in an unavailable language.
  217. $unavailable_language = 'xx';
  218. db_insert($this->table)
  219. ->fields(array('entity_type', 'bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'language'))
  220. ->values(array($entity_type, $this->instance['bundle'], 0, 0, 0, 0, $unavailable_language))
  221. ->execute();
  222. $count = db_select($this->table)
  223. ->countQuery()
  224. ->execute()
  225. ->fetchField();
  226. $this->assertEqual($count, 1, 'Field translation in an unavailable language saved.');
  227. // Again add some real data.
  228. $entity->{$this->field_name}[$langcode] = array(0 => array('value' => 1));
  229. field_attach_insert($entity_type, $entity);
  230. $count = db_select($this->table)
  231. ->countQuery()
  232. ->execute()
  233. ->fetchField();
  234. $this->assertEqual($count, 2, 'Field data saved.');
  235. // Update: Field translation is missing but field is not empty. Translation
  236. // data should survive.
  237. $entity->{$this->field_name}[$unavailable_language] = array(mt_rand(1, 127));
  238. unset($entity->{$this->field_name}[$langcode]);
  239. field_attach_update($entity_type, $entity);
  240. $count = db_select($this->table)
  241. ->countQuery()
  242. ->execute()
  243. ->fetchField();
  244. $this->assertEqual($count, 2, 'Missing field translation leaves data in table.');
  245. // Update: Field translation is NULL but field is not empty. Translation
  246. // data should be wiped.
  247. $entity->{$this->field_name}[$langcode] = NULL;
  248. field_attach_update($entity_type, $entity);
  249. $count = db_select($this->table)
  250. ->countQuery()
  251. ->execute()
  252. ->fetchField();
  253. $this->assertEqual($count, 1, 'NULL field translation is wiped.');
  254. }
  255. /**
  256. * Test trying to update a field with data.
  257. */
  258. function testUpdateFieldSchemaWithData() {
  259. // Create a decimal 5.2 field and add some data.
  260. $field = array('field_name' => 'decimal52', 'type' => 'number_decimal', 'settings' => array('precision' => 5, 'scale' => 2));
  261. $field = field_create_field($field);
  262. $instance = array('field_name' => 'decimal52', 'entity_type' => 'test_entity', 'bundle' => 'test_bundle');
  263. $instance = field_create_instance($instance);
  264. $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  265. $entity->decimal52[LANGUAGE_NONE][0]['value'] = '1.235';
  266. field_attach_insert('test_entity', $entity);
  267. // Attempt to update the field in a way that would work without data.
  268. $field['settings']['scale'] = 3;
  269. try {
  270. field_update_field($field);
  271. $this->fail(t('Cannot update field schema with data.'));
  272. }
  273. catch (FieldException $e) {
  274. $this->pass(t('Cannot update field schema with data.'));
  275. }
  276. }
  277. /**
  278. * Test that failure to create fields is handled gracefully.
  279. */
  280. function testFieldUpdateFailure() {
  281. // Create a text field.
  282. $field = array('field_name' => 'test_text', 'type' => 'text', 'settings' => array('max_length' => 255));
  283. $field = field_create_field($field);
  284. // Attempt to update the field in a way that would break the storage.
  285. $prior_field = $field;
  286. $field['settings']['max_length'] = -1;
  287. try {
  288. field_update_field($field);
  289. $this->fail(t('Update succeeded.'));
  290. }
  291. catch (Exception $e) {
  292. $this->pass(t('Update properly failed.'));
  293. }
  294. // Ensure that the field tables are still there.
  295. foreach (_field_sql_storage_schema($prior_field) as $table_name => $table_info) {
  296. $this->assertTrue(db_table_exists($table_name), format_string('Table %table exists.', array('%table' => $table_name)));
  297. }
  298. }
  299. /**
  300. * Test adding and removing indexes while data is present.
  301. */
  302. function testFieldUpdateIndexesWithData() {
  303. // Create a decimal field.
  304. $field_name = 'testfield';
  305. $field = array('field_name' => $field_name, 'type' => 'text');
  306. $field = field_create_field($field);
  307. $instance = array('field_name' => $field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle');
  308. $instance = field_create_instance($instance);
  309. $tables = array(_field_sql_storage_tablename($field), _field_sql_storage_revision_tablename($field));
  310. // Verify the indexes we will create do not exist yet.
  311. foreach ($tables as $table) {
  312. $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value'), format_string("No index named value exists in %table", array('%table' => $table)));
  313. $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value_format'), format_string("No index named value_format exists in %table", array('%table' => $table)));
  314. }
  315. // Add data so the table cannot be dropped.
  316. $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  317. $entity->{$field_name}[LANGUAGE_NONE][0]['value'] = 'field data';
  318. field_attach_insert('test_entity', $entity);
  319. // Add an index
  320. $field = array('field_name' => $field_name, 'indexes' => array('value' => array(array('value', 255))));
  321. field_update_field($field);
  322. foreach ($tables as $table) {
  323. $this->assertTrue(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value"), format_string("Index on value created in %table", array('%table' => $table)));
  324. }
  325. // Add a different index, removing the existing custom one.
  326. $field = array('field_name' => $field_name, 'indexes' => array('value_format' => array(array('value', 127), array('format', 127))));
  327. field_update_field($field);
  328. foreach ($tables as $table) {
  329. $this->assertTrue(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value_format"), format_string("Index on value_format created in %table", array('%table' => $table)));
  330. $this->assertFalse(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value"), format_string("Index on value removed in %table", array('%table' => $table)));
  331. }
  332. // Verify that the tables were not dropped.
  333. $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  334. field_attach_load('test_entity', array(0 => $entity));
  335. $this->assertEqual($entity->{$field_name}[LANGUAGE_NONE][0]['value'], 'field data', "Index changes performed without dropping the tables");
  336. }
  337. /**
  338. * Test the storage details.
  339. */
  340. function testFieldStorageDetails() {
  341. $current = _field_sql_storage_tablename($this->field);
  342. $revision = _field_sql_storage_revision_tablename($this->field);
  343. // Retrieve the field and instance with field_info so the storage details are attached.
  344. $field = field_info_field($this->field['field_name']);
  345. $instance = field_info_instance($this->instance['entity_type'], $this->instance['field_name'], $this->instance['bundle']);
  346. // The storage details are indexed by a storage engine type.
  347. $this->assertTrue(array_key_exists('sql', $field['storage']['details']), 'The storage type is SQL.');
  348. // The SQL details are indexed by table name.
  349. $details = $field['storage']['details']['sql'];
  350. $this->assertTrue(array_key_exists($current, $details[FIELD_LOAD_CURRENT]), 'Table name is available in the instance array.');
  351. $this->assertTrue(array_key_exists($revision, $details[FIELD_LOAD_REVISION]), 'Revision table name is available in the instance array.');
  352. // Test current and revision storage details together because the columns
  353. // are the same.
  354. foreach ((array) $this->field['columns'] as $column_name => $attributes) {
  355. $storage_column_name = _field_sql_storage_columnname($this->field['field_name'], $column_name);
  356. $this->assertEqual($details[FIELD_LOAD_CURRENT][$current][$column_name], $storage_column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => $current)));
  357. $this->assertEqual($details[FIELD_LOAD_REVISION][$revision][$column_name], $storage_column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => $revision)));
  358. }
  359. }
  360. /**
  361. * Test foreign key support.
  362. */
  363. function testFieldSqlStorageForeignKeys() {
  364. // Create a 'shape' field, with a configurable foreign key (see
  365. // field_test_field_schema()).
  366. $field_name = 'testfield';
  367. $foreign_key_name = 'shape';
  368. $field = array('field_name' => $field_name, 'type' => 'shape', 'settings' => array('foreign_key_name' => $foreign_key_name));
  369. field_create_field($field);
  370. // Retrieve the field definition and check that the foreign key is in place.
  371. $field = field_info_field($field_name);
  372. $this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name preserved through CRUD');
  373. $this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name preserved through CRUD');
  374. // Update the field settings, it should update the foreign key definition
  375. // too.
  376. $foreign_key_name = 'color';
  377. $field['settings']['foreign_key_name'] = $foreign_key_name;
  378. field_update_field($field);
  379. // Retrieve the field definition and check that the foreign key is in place.
  380. $field = field_info_field($field_name);
  381. $this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name modified after update');
  382. $this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name modified after update');
  383. // Now grab the SQL schema and verify that too.
  384. $schema = drupal_get_schema(_field_sql_storage_tablename($field), TRUE);
  385. $this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema');
  386. $foreign_key = reset($schema['foreign keys']);
  387. $foreign_key_column = _field_sql_storage_columnname($field['field_name'], $foreign_key_name);
  388. $this->assertEqual($foreign_key['table'], $foreign_key_name, 'Foreign key table name preserved in the schema');
  389. $this->assertEqual($foreign_key['columns'][$foreign_key_column], 'id', 'Foreign key column name preserved in the schema');
  390. }
  391. /**
  392. * Test handling multiple conditions on one column of a field.
  393. *
  394. * Tests both the result and the complexity of the query.
  395. */
  396. function testFieldSqlStorageMultipleConditionsSameColumn() {
  397. $entity = field_test_create_stub_entity(NULL, NULL);
  398. $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 1);
  399. field_test_entity_save($entity);
  400. $entity = field_test_create_stub_entity(NULL, NULL);
  401. $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 2);
  402. field_test_entity_save($entity);
  403. $entity = field_test_create_stub_entity(NULL, NULL);
  404. $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 3);
  405. field_test_entity_save($entity);
  406. $query = new EntityFieldQuery();
  407. // This tag causes field_test_query_store_global_test_query_alter() to be
  408. // invoked so that the query can be tested.
  409. $query->addTag('store_global_test_query');
  410. $query->entityCondition('entity_type', 'test_entity');
  411. $query->entityCondition('bundle', 'test_bundle');
  412. $query->fieldCondition($this->field_name, 'value', 1, '<>', 0, LANGUAGE_NONE);
  413. $query->fieldCondition($this->field_name, 'value', 2, '<>', 0, LANGUAGE_NONE);
  414. $result = field_sql_storage_field_storage_query($query);
  415. // Test the results.
  416. $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
  417. // Test the complexity of the query.
  418. $query = $GLOBALS['test_query'];
  419. $this->assertNotNull($query, 'Precondition: the query should be available');
  420. $tables = $query->getTables();
  421. $this->assertEqual(1, count($tables), 'The query contains just one table.');
  422. // Clean up.
  423. unset($GLOBALS['test_query']);
  424. }
  425. /**
  426. * Test handling multiple conditions on multiple columns of one field.
  427. *
  428. * Tests both the result and the complexity of the query.
  429. */
  430. function testFieldSqlStorageMultipleConditionsDifferentColumns() {
  431. // Create the multi-column shape field
  432. $field_name = strtolower($this->randomName());
  433. $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4);
  434. $field = field_create_field($field);
  435. $instance = array(
  436. 'field_name' => $field_name,
  437. 'entity_type' => 'test_entity',
  438. 'bundle' => 'test_bundle'
  439. );
  440. $instance = field_create_instance($instance);
  441. $entity = field_test_create_stub_entity(NULL, NULL);
  442. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'X');
  443. field_test_entity_save($entity);
  444. $entity = field_test_create_stub_entity(NULL, NULL);
  445. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'B', 'color' => 'X');
  446. field_test_entity_save($entity);
  447. $entity = field_test_create_stub_entity(NULL, NULL);
  448. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'Y');
  449. field_test_entity_save($entity);
  450. $query = new EntityFieldQuery();
  451. // This tag causes field_test_query_store_global_test_query_alter() to be
  452. // invoked so that the query can be tested.
  453. $query->addTag('store_global_test_query');
  454. $query->entityCondition('entity_type', 'test_entity');
  455. $query->entityCondition('bundle', 'test_bundle');
  456. $query->fieldCondition($field_name, 'shape', 'B', '=', 'something', LANGUAGE_NONE);
  457. $query->fieldCondition($field_name, 'color', 'X', '=', 'something', LANGUAGE_NONE);
  458. $result = field_sql_storage_field_storage_query($query);
  459. // Test the results.
  460. $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
  461. // Test the complexity of the query.
  462. $query = $GLOBALS['test_query'];
  463. $this->assertNotNull($query, 'Precondition: the query should be available');
  464. $tables = $query->getTables();
  465. $this->assertEqual(1, count($tables), 'The query contains just one table.');
  466. // Clean up.
  467. unset($GLOBALS['test_query']);
  468. }
  469. /**
  470. * Test handling multiple conditions on multiple columns of one field for multiple languages.
  471. *
  472. * Tests both the result and the complexity of the query.
  473. */
  474. function testFieldSqlStorageMultipleConditionsDifferentColumnsMultipleLanguages() {
  475. field_test_entity_info_translatable('test_entity', TRUE);
  476. // Create the multi-column shape field
  477. $field_name = strtolower($this->randomName());
  478. $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4, 'translatable' => TRUE);
  479. $field = field_create_field($field);
  480. $instance = array(
  481. 'field_name' => $field_name,
  482. 'entity_type' => 'test_entity',
  483. 'bundle' => 'test_bundle',
  484. 'settings' => array(
  485. // Prevent warning from field_test_field_load().
  486. 'test_hook_field_load' => FALSE,
  487. ),
  488. );
  489. $instance = field_create_instance($instance);
  490. $entity = field_test_create_stub_entity(NULL, NULL);
  491. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'X');
  492. $entity->{$field_name}['en'][0] = array('shape' => 'B', 'color' => 'Y');
  493. field_test_entity_save($entity);
  494. $entity = field_test_entity_test_load($entity->ftid);
  495. $query = new EntityFieldQuery();
  496. // This tag causes field_test_query_store_global_test_query_alter() to be
  497. // invoked so that the query can be tested.
  498. $query->addTag('store_global_test_query');
  499. $query->entityCondition('entity_type', 'test_entity');
  500. $query->entityCondition('bundle', 'test_bundle');
  501. $query->fieldCondition($field_name, 'color', 'X', '=', NULL, LANGUAGE_NONE);
  502. $query->fieldCondition($field_name, 'shape', 'B', '=', NULL, 'en');
  503. $result = field_sql_storage_field_storage_query($query);
  504. // Test the results.
  505. $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
  506. // Test the complexity of the query.
  507. $query = $GLOBALS['test_query'];
  508. $this->assertNotNull($query, 'Precondition: the query should be available');
  509. $tables = $query->getTables();
  510. $this->assertEqual(2, count($tables), 'The query contains two tables.');
  511. // Clean up.
  512. unset($GLOBALS['test_query']);
  513. }
  514. }