field_sql_storage.test 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. * Tests the expected return values of _field_sql_storage_write_compare().
  257. */
  258. public function testFieldCompareDataModification() {
  259. $langcode = LANGUAGE_NONE;
  260. $field_info = field_info_field($this->field_name);
  261. // Make sure we have 2 sample field values that are unique.
  262. $value1 = 0;
  263. $value2 = 0;
  264. while ($value1 == $value2) {
  265. $value1 = mt_rand();
  266. $value2 = (string) mt_rand();
  267. }
  268. // Create the 2 entities to compare.
  269. $entity = field_test_create_stub_entity();
  270. $entity->{$this->field_name}[$langcode][]['value'] = $value1;
  271. $entity1 = clone $entity;
  272. $entity2 = clone $entity;
  273. // Make sure that it correctly compares identical entities.
  274. $this->assert(!_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'The entities are identical.');
  275. // Compare to an empty object.
  276. $this->assert(_field_sql_storage_write_compare($field_info, $entity1, new stdClass()), 'The entity is not the same as an empty object.');
  277. // Change one of the values.
  278. $entity2->{$this->field_name}[$langcode][0]['value'] = $value2;
  279. $this->assert(_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'The values are not the same.');
  280. // Reset $entity2.
  281. $entity2 = clone $entity;
  282. // Duplicate the value on one of the entities.
  283. $entity1->{$this->field_name}[$langcode][]['value'] = $value1;
  284. $this->assert(_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'The fields do not have the same number of values.');
  285. // Add a second value to both entities.
  286. $entity2->{$this->field_name}[$langcode][]['value'] = $value2;
  287. $this->assert(_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'The values are not the same.');
  288. // Replace the array containing the value with the actual value.
  289. $entity2->{$this->field_name}[$langcode] = $entity2->{$this->field_name}[$langcode][0];
  290. $this->assert(_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'The array to hold field values is replaced by the value.');
  291. // Null one value.
  292. $entity2->{$this->field_name}[$langcode] = NULL;
  293. $this->assert(_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'One field is NULL and the other is not.');
  294. // Null both values.
  295. $entity1->{$this->field_name}[$langcode] = NULL;
  296. $this->assert(!_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'Both fields are NULL.');
  297. // Unset one of the fields.
  298. unset($entity2->{$this->field_name});
  299. $this->assert(_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'One field structure is unset.');
  300. // Unset both of the fields.
  301. unset($entity1->{$this->field_name});
  302. $this->assert(!_field_sql_storage_write_compare($field_info, $entity1, $entity2), 'Both field structures are unset.');
  303. }
  304. /**
  305. * Test trying to update a field with data.
  306. */
  307. function testUpdateFieldSchemaWithData() {
  308. // Create a decimal 5.2 field and add some data.
  309. $field = array('field_name' => 'decimal52', 'type' => 'number_decimal', 'settings' => array('precision' => 5, 'scale' => 2));
  310. $field = field_create_field($field);
  311. $instance = array('field_name' => 'decimal52', 'entity_type' => 'test_entity', 'bundle' => 'test_bundle');
  312. $instance = field_create_instance($instance);
  313. $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  314. $entity->decimal52[LANGUAGE_NONE][0]['value'] = '1.235';
  315. field_attach_insert('test_entity', $entity);
  316. // Attempt to update the field in a way that would work without data.
  317. $field['settings']['scale'] = 3;
  318. try {
  319. field_update_field($field);
  320. $this->fail(t('Cannot update field schema with data.'));
  321. }
  322. catch (FieldException $e) {
  323. $this->pass(t('Cannot update field schema with data.'));
  324. }
  325. }
  326. /**
  327. * Test that failure to create fields is handled gracefully.
  328. */
  329. function testFieldUpdateFailure() {
  330. // Create a text field.
  331. $field = array('field_name' => 'test_text', 'type' => 'text', 'settings' => array('max_length' => 255));
  332. $field = field_create_field($field);
  333. // Attempt to update the field in a way that would break the storage. The
  334. // parenthesis suffix is needed because SQLite has *very* relaxed rules for
  335. // data types, so we actually need to provide an invalid SQL syntax in order
  336. // to break it.
  337. // @see https://www.sqlite.org/datatype3.html
  338. $prior_field = $field;
  339. $field['settings']['max_length'] = '-1)';
  340. try {
  341. field_update_field($field);
  342. $this->fail(t('Update succeeded.'));
  343. }
  344. catch (Exception $e) {
  345. $this->pass(t('Update properly failed.'));
  346. }
  347. // Ensure that the field tables are still there.
  348. foreach (_field_sql_storage_schema($prior_field) as $table_name => $table_info) {
  349. $this->assertTrue(db_table_exists($table_name), format_string('Table %table exists.', array('%table' => $table_name)));
  350. }
  351. }
  352. /**
  353. * Test adding and removing indexes while data is present.
  354. */
  355. function testFieldUpdateIndexesWithData() {
  356. // Create a decimal field.
  357. $field_name = 'testfield';
  358. $field = array('field_name' => $field_name, 'type' => 'text');
  359. $field = field_create_field($field);
  360. $instance = array('field_name' => $field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle');
  361. $instance = field_create_instance($instance);
  362. $tables = array(_field_sql_storage_tablename($field), _field_sql_storage_revision_tablename($field));
  363. // Verify the indexes we will create do not exist yet.
  364. foreach ($tables as $table) {
  365. $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value'), format_string("No index named value exists in %table", array('%table' => $table)));
  366. $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value_format'), format_string("No index named value_format exists in %table", array('%table' => $table)));
  367. }
  368. // Add data so the table cannot be dropped.
  369. $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  370. $entity->{$field_name}[LANGUAGE_NONE][0]['value'] = 'field data';
  371. field_attach_insert('test_entity', $entity);
  372. // Add an index
  373. $field = array('field_name' => $field_name, 'indexes' => array('value' => array(array('value', 255))));
  374. field_update_field($field);
  375. foreach ($tables as $table) {
  376. $this->assertTrue(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value"), format_string("Index on value created in %table", array('%table' => $table)));
  377. }
  378. // Add a different index, removing the existing custom one.
  379. $field = array('field_name' => $field_name, 'indexes' => array('value_format' => array(array('value', 127), array('format', 127))));
  380. field_update_field($field);
  381. foreach ($tables as $table) {
  382. $this->assertTrue(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value_format"), format_string("Index on value_format created in %table", array('%table' => $table)));
  383. $this->assertFalse(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value"), format_string("Index on value removed in %table", array('%table' => $table)));
  384. }
  385. // Verify that the tables were not dropped.
  386. $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  387. field_attach_load('test_entity', array(0 => $entity));
  388. $this->assertEqual($entity->{$field_name}[LANGUAGE_NONE][0]['value'], 'field data', "Index changes performed without dropping the tables");
  389. }
  390. /**
  391. * Test the storage details.
  392. */
  393. function testFieldStorageDetails() {
  394. $current = _field_sql_storage_tablename($this->field);
  395. $revision = _field_sql_storage_revision_tablename($this->field);
  396. // Retrieve the field and instance with field_info so the storage details are attached.
  397. $field = field_info_field($this->field['field_name']);
  398. $instance = field_info_instance($this->instance['entity_type'], $this->instance['field_name'], $this->instance['bundle']);
  399. // The storage details are indexed by a storage engine type.
  400. $this->assertTrue(array_key_exists('sql', $field['storage']['details']), 'The storage type is SQL.');
  401. // The SQL details are indexed by table name.
  402. $details = $field['storage']['details']['sql'];
  403. $this->assertTrue(array_key_exists($current, $details[FIELD_LOAD_CURRENT]), 'Table name is available in the instance array.');
  404. $this->assertTrue(array_key_exists($revision, $details[FIELD_LOAD_REVISION]), 'Revision table name is available in the instance array.');
  405. // Test current and revision storage details together because the columns
  406. // are the same.
  407. foreach ((array) $this->field['columns'] as $column_name => $attributes) {
  408. $storage_column_name = _field_sql_storage_columnname($this->field['field_name'], $column_name);
  409. $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)));
  410. $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)));
  411. }
  412. }
  413. /**
  414. * Test foreign key support.
  415. */
  416. function testFieldSqlStorageForeignKeys() {
  417. // Create a 'shape' field, with a configurable foreign key (see
  418. // field_test_field_schema()).
  419. $field_name = 'testfield';
  420. $foreign_key_name = 'shape';
  421. $field = array('field_name' => $field_name, 'type' => 'shape', 'settings' => array('foreign_key_name' => $foreign_key_name));
  422. field_create_field($field);
  423. // Retrieve the field definition and check that the foreign key is in place.
  424. $field = field_info_field($field_name);
  425. $this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name preserved through CRUD');
  426. $this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name preserved through CRUD');
  427. // Update the field settings, it should update the foreign key definition
  428. // too.
  429. $foreign_key_name = 'color';
  430. $field['settings']['foreign_key_name'] = $foreign_key_name;
  431. field_update_field($field);
  432. // Retrieve the field definition and check that the foreign key is in place.
  433. $field = field_info_field($field_name);
  434. $this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name modified after update');
  435. $this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name modified after update');
  436. // Now grab the SQL schema and verify that too.
  437. $schema = drupal_get_schema(_field_sql_storage_tablename($field), TRUE);
  438. $this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema');
  439. $foreign_key = reset($schema['foreign keys']);
  440. $foreign_key_column = _field_sql_storage_columnname($field['field_name'], $foreign_key_name);
  441. $this->assertEqual($foreign_key['table'], $foreign_key_name, 'Foreign key table name preserved in the schema');
  442. $this->assertEqual($foreign_key['columns'][$foreign_key_column], 'id', 'Foreign key column name preserved in the schema');
  443. }
  444. /**
  445. * Test handling multiple conditions on one column of a field.
  446. *
  447. * Tests both the result and the complexity of the query.
  448. */
  449. function testFieldSqlStorageMultipleConditionsSameColumn() {
  450. $entity = field_test_create_stub_entity(NULL, NULL);
  451. $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 1);
  452. field_test_entity_save($entity);
  453. $entity = field_test_create_stub_entity(NULL, NULL);
  454. $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 2);
  455. field_test_entity_save($entity);
  456. $entity = field_test_create_stub_entity(NULL, NULL);
  457. $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 3);
  458. field_test_entity_save($entity);
  459. $query = new EntityFieldQuery();
  460. // This tag causes field_test_query_store_global_test_query_alter() to be
  461. // invoked so that the query can be tested.
  462. $query->addTag('store_global_test_query');
  463. $query->entityCondition('entity_type', 'test_entity');
  464. $query->entityCondition('bundle', 'test_bundle');
  465. $query->fieldCondition($this->field_name, 'value', 1, '<>', 0, LANGUAGE_NONE);
  466. $query->fieldCondition($this->field_name, 'value', 2, '<>', 0, LANGUAGE_NONE);
  467. $result = field_sql_storage_field_storage_query($query);
  468. // Test the results.
  469. $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
  470. // Test the complexity of the query.
  471. $query = $GLOBALS['test_query'];
  472. $this->assertNotNull($query, 'Precondition: the query should be available');
  473. $tables = $query->getTables();
  474. $this->assertEqual(1, count($tables), 'The query contains just one table.');
  475. // Clean up.
  476. unset($GLOBALS['test_query']);
  477. }
  478. /**
  479. * Test handling multiple conditions on multiple columns of one field.
  480. *
  481. * Tests both the result and the complexity of the query.
  482. */
  483. function testFieldSqlStorageMultipleConditionsDifferentColumns() {
  484. // Create the multi-column shape field
  485. $field_name = strtolower($this->randomName());
  486. $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4);
  487. $field = field_create_field($field);
  488. $instance = array(
  489. 'field_name' => $field_name,
  490. 'entity_type' => 'test_entity',
  491. 'bundle' => 'test_bundle'
  492. );
  493. $instance = field_create_instance($instance);
  494. $entity = field_test_create_stub_entity(NULL, NULL);
  495. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'X');
  496. field_test_entity_save($entity);
  497. $entity = field_test_create_stub_entity(NULL, NULL);
  498. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'B', 'color' => 'X');
  499. field_test_entity_save($entity);
  500. $entity = field_test_create_stub_entity(NULL, NULL);
  501. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'Y');
  502. field_test_entity_save($entity);
  503. $query = new EntityFieldQuery();
  504. // This tag causes field_test_query_store_global_test_query_alter() to be
  505. // invoked so that the query can be tested.
  506. $query->addTag('store_global_test_query');
  507. $query->entityCondition('entity_type', 'test_entity');
  508. $query->entityCondition('bundle', 'test_bundle');
  509. $query->fieldCondition($field_name, 'shape', 'B', '=', 'something', LANGUAGE_NONE);
  510. $query->fieldCondition($field_name, 'color', 'X', '=', 'something', LANGUAGE_NONE);
  511. $result = field_sql_storage_field_storage_query($query);
  512. // Test the results.
  513. $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
  514. // Test the complexity of the query.
  515. $query = $GLOBALS['test_query'];
  516. $this->assertNotNull($query, 'Precondition: the query should be available');
  517. $tables = $query->getTables();
  518. $this->assertEqual(1, count($tables), 'The query contains just one table.');
  519. // Clean up.
  520. unset($GLOBALS['test_query']);
  521. }
  522. /**
  523. * Test handling multiple conditions on multiple columns of one field for multiple languages.
  524. *
  525. * Tests both the result and the complexity of the query.
  526. */
  527. function testFieldSqlStorageMultipleConditionsDifferentColumnsMultipleLanguages() {
  528. field_test_entity_info_translatable('test_entity', TRUE);
  529. // Create the multi-column shape field
  530. $field_name = strtolower($this->randomName());
  531. $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4, 'translatable' => TRUE);
  532. $field = field_create_field($field);
  533. $instance = array(
  534. 'field_name' => $field_name,
  535. 'entity_type' => 'test_entity',
  536. 'bundle' => 'test_bundle',
  537. 'settings' => array(
  538. // Prevent warning from field_test_field_load().
  539. 'test_hook_field_load' => FALSE,
  540. ),
  541. );
  542. $instance = field_create_instance($instance);
  543. $entity = field_test_create_stub_entity(NULL, NULL);
  544. $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'X');
  545. $entity->{$field_name}['en'][0] = array('shape' => 'B', 'color' => 'Y');
  546. field_test_entity_save($entity);
  547. $entity = field_test_entity_test_load($entity->ftid);
  548. $query = new EntityFieldQuery();
  549. // This tag causes field_test_query_store_global_test_query_alter() to be
  550. // invoked so that the query can be tested.
  551. $query->addTag('store_global_test_query');
  552. $query->entityCondition('entity_type', 'test_entity');
  553. $query->entityCondition('bundle', 'test_bundle');
  554. $query->fieldCondition($field_name, 'color', 'X', '=', NULL, LANGUAGE_NONE);
  555. $query->fieldCondition($field_name, 'shape', 'B', '=', NULL, 'en');
  556. $result = field_sql_storage_field_storage_query($query);
  557. // Test the results.
  558. $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
  559. // Test the complexity of the query.
  560. $query = $GLOBALS['test_query'];
  561. $this->assertNotNull($query, 'Precondition: the query should be available');
  562. $tables = $query->getTables();
  563. $this->assertEqual(2, count($tables), 'The query contains two tables.');
  564. // Clean up.
  565. unset($GLOBALS['test_query']);
  566. }
  567. }