Projet

Général

Profil

Paste
Télécharger (25,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / field / modules / field_sql_storage / field_sql_storage.test @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for field_sql_storage.module.
6
 *
7
 * Field_sql_storage.module implements the default back-end storage plugin
8
 * for the Field Strage API.
9
 */
10

    
11
/**
12
 * Tests field storage.
13
 */
14
class FieldSqlStorageTestCase extends DrupalWebTestCase {
15
  public static function getInfo() {
16
    return array(
17
      'name'  => 'Field SQL storage tests',
18
      'description'  => "Test field SQL storage module.",
19
      'group' => 'Field API'
20
    );
21
  }
22

    
23
  function setUp() {
24
    parent::setUp('field_sql_storage', 'field', 'field_test', 'text');
25
    $this->field_name = strtolower($this->randomName());
26
    $this->field = array('field_name' => $this->field_name, 'type' => 'test_field', 'cardinality' => 4);
27
    $this->field = field_create_field($this->field);
28
    $this->instance = array(
29
      'field_name' => $this->field_name,
30
      'entity_type' => 'test_entity',
31
      'bundle' => 'test_bundle'
32
    );
33
    $this->instance = field_create_instance($this->instance);
34
    $this->table = _field_sql_storage_tablename($this->field);
35
    $this->revision_table = _field_sql_storage_revision_tablename($this->field);
36

    
37
  }
38

    
39
  /**
40
   * Uses the mysql tables and records to verify
41
   * field_load_revision works correctly.
42
   */
43
  function testFieldAttachLoad() {
44
    $entity_type = 'test_entity';
45
    $eid = 0;
46
    $langcode = LANGUAGE_NONE;
47

    
48
    $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $this->field_name . '_value');
49

    
50
    // Insert data for four revisions to the field revisions table
51
    $query = db_insert($this->revision_table)->fields($columns);
52
    for ($evid = 0; $evid < 4; ++$evid) {
53
      $values[$evid] = array();
54
      // Note: we insert one extra value ('<=' instead of '<').
55
      for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
56
        $value = mt_rand(1, 127);
57
        $values[$evid][] = $value;
58
        $query->values(array($entity_type, $eid, $evid, $delta, $langcode, $value));
59
      }
60
    }
61
    $query->execute();
62

    
63
    // Insert data for the "most current revision" into the field table
64
    $query = db_insert($this->table)->fields($columns);
65
    foreach ($values[0] as $delta => $value) {
66
      $query->values(array($entity_type, $eid, 0, $delta, $langcode, $value));
67
    }
68
    $query->execute();
69

    
70
    // Load the "most current revision"
71
    $entity = field_test_create_stub_entity($eid, 0, $this->instance['bundle']);
72
    field_attach_load($entity_type, array($eid => $entity));
73
    foreach ($values[0] as $delta => $value) {
74
      if ($delta < $this->field['cardinality']) {
75
        $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $value, "Value $delta is loaded correctly for current revision");
76
      }
77
      else {
78
        $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}[$langcode]), "No extraneous value gets loaded for current revision.");
79
      }
80
    }
81

    
82
    // Load every revision
83
    for ($evid = 0; $evid < 4; ++$evid) {
84
      $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
85
      field_attach_load_revision($entity_type, array($eid => $entity));
86
      foreach ($values[$evid] as $delta => $value) {
87
        if ($delta < $this->field['cardinality']) {
88
          $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $value, "Value $delta for revision $evid is loaded correctly");
89
        }
90
        else {
91
          $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}[$langcode]), "No extraneous value gets loaded for revision $evid.");
92
        }
93
      }
94
    }
95

    
96
    // Add a translation in an unavailable language and verify it is not loaded.
97
    $eid = $evid = 1;
98
    $unavailable_language = 'xx';
99
    $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
100
    $values = array($entity_type, $eid, $evid, 0, $unavailable_language, mt_rand(1, 127));
101
    db_insert($this->table)->fields($columns)->values($values)->execute();
102
    db_insert($this->revision_table)->fields($columns)->values($values)->execute();
103
    field_attach_load($entity_type, array($eid => $entity));
104
    $this->assertFalse(array_key_exists($unavailable_language, $entity->{$this->field_name}), 'Field translation in an unavailable language ignored');
105
  }
106

    
107
  /**
108
   * Reads mysql to verify correct data is
109
   * written when using insert and update.
110
   */
111
  function testFieldAttachInsertAndUpdate() {
112
    $entity_type = 'test_entity';
113
    $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
114
    $langcode = LANGUAGE_NONE;
115

    
116
    // Test insert.
117
    $values = array();
118
    // Note: we try to insert one extra value ('<=' instead of '<').
119
    // TODO : test empty values filtering and "compression" (store consecutive deltas).
120
    for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
121
      $values[$delta]['value'] = mt_rand(1, 127);
122
    }
123
    $entity->{$this->field_name}[$langcode] = $rev_values[0] = $values;
124
    field_attach_insert($entity_type, $entity);
125

    
126
    $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
127
    foreach ($values as $delta => $value) {
128
      if ($delta < $this->field['cardinality']) {
129
        $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], format_string("Value %delta is inserted correctly", array('%delta' => $delta)));
130
      }
131
      else {
132
        $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets inserted.");
133
      }
134
    }
135

    
136
    // Test update.
137
    $entity = field_test_create_stub_entity(0, 1, $this->instance['bundle']);
138
    $values = array();
139
    // Note: we try to update one extra value ('<=' instead of '<').
140
    for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
141
      $values[$delta]['value'] = mt_rand(1, 127);
142
    }
143
    $entity->{$this->field_name}[$langcode] = $rev_values[1] = $values;
144
    field_attach_update($entity_type, $entity);
145
    $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
146
    foreach ($values as $delta => $value) {
147
      if ($delta < $this->field['cardinality']) {
148
        $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], format_string("Value %delta is updated correctly", array('%delta' => $delta)));
149
      }
150
      else {
151
        $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets updated.");
152
      }
153
    }
154

    
155
    // Check that data for both revisions are in the revision table.
156
    // We make sure each value is stored correctly, then unset it.
157
    // When an entire revision's values are unset (remembering that we
158
    // put one extra value in $values per revision), unset the entire
159
    // revision. Then, if $rev_values is empty at the end, all
160
    // revision data was found.
161
    $results = db_select($this->revision_table, 't')->fields('t')->execute();
162
    foreach ($results as $row) {
163
      $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");
164
      unset($rev_values[$row->revision_id][$row->delta]);
165
      if (count($rev_values[$row->revision_id]) == 1) {
166
        unset($rev_values[$row->revision_id]);
167
      }
168
    }
169
    $this->assertTrue(empty($rev_values), "All values for all revisions are stored in revision table {$this->revision_table}");
170

    
171
    // Check that update leaves the field data untouched if
172
    // $entity->{$field_name} is absent.
173
    unset($entity->{$this->field_name});
174
    field_attach_update($entity_type, $entity);
175
    $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
176
    foreach ($values as $delta => $value) {
177
      if ($delta < $this->field['cardinality']) {
178
        $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)));
179
      }
180
    }
181

    
182
    // Check that update with an empty $entity->$field_name empties the field.
183
    $entity->{$this->field_name} = NULL;
184
    field_attach_update($entity_type, $entity);
185
    $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
186
    $this->assertEqual(count($rows), 0, "Update with an empty field_name entry empties the field.");
187
  }
188

    
189
  /**
190
   * Tests insert and update with missing or NULL fields.
191
   */
192
  function testFieldAttachSaveMissingData() {
193
    $entity_type = 'test_entity';
194
    $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
195
    $langcode = LANGUAGE_NONE;
196

    
197
    // Insert: Field is missing
198
    field_attach_insert($entity_type, $entity);
199
    $count = db_select($this->table)
200
      ->countQuery()
201
      ->execute()
202
      ->fetchField();
203
    $this->assertEqual($count, 0, 'Missing field results in no inserts');
204

    
205
    // Insert: Field is NULL
206
    $entity->{$this->field_name} = NULL;
207
    field_attach_insert($entity_type, $entity);
208
    $count = db_select($this->table)
209
      ->countQuery()
210
      ->execute()
211
      ->fetchField();
212
    $this->assertEqual($count, 0, 'NULL field results in no inserts');
213

    
214
    // Add some real data
215
    $entity->{$this->field_name}[$langcode] = array(0 => array('value' => 1));
216
    field_attach_insert($entity_type, $entity);
217
    $count = db_select($this->table)
218
      ->countQuery()
219
      ->execute()
220
      ->fetchField();
221
    $this->assertEqual($count, 1, 'Field data saved');
222

    
223
    // Update: Field is missing. Data should survive.
224
    unset($entity->{$this->field_name});
225
    field_attach_update($entity_type, $entity);
226
    $count = db_select($this->table)
227
      ->countQuery()
228
      ->execute()
229
      ->fetchField();
230
    $this->assertEqual($count, 1, 'Missing field leaves data in table');
231

    
232
    // Update: Field is NULL. Data should be wiped.
233
    $entity->{$this->field_name} = NULL;
234
    field_attach_update($entity_type, $entity);
235
    $count = db_select($this->table)
236
      ->countQuery()
237
      ->execute()
238
      ->fetchField();
239
    $this->assertEqual($count, 0, 'NULL field leaves no data in table');
240

    
241
    // Add a translation in an unavailable language.
242
    $unavailable_language = 'xx';
243
    db_insert($this->table)
244
      ->fields(array('entity_type', 'bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'language'))
245
      ->values(array($entity_type, $this->instance['bundle'], 0, 0, 0, 0, $unavailable_language))
246
      ->execute();
247
    $count = db_select($this->table)
248
      ->countQuery()
249
      ->execute()
250
      ->fetchField();
251
    $this->assertEqual($count, 1, 'Field translation in an unavailable language saved.');
252

    
253
    // Again add some real data.
254
    $entity->{$this->field_name}[$langcode] = array(0 => array('value' => 1));
255
    field_attach_insert($entity_type, $entity);
256
    $count = db_select($this->table)
257
      ->countQuery()
258
      ->execute()
259
      ->fetchField();
260
    $this->assertEqual($count, 2, 'Field data saved.');
261

    
262
    // Update: Field translation is missing but field is not empty. Translation
263
    // data should survive.
264
    $entity->{$this->field_name}[$unavailable_language] = array(mt_rand(1, 127));
265
    unset($entity->{$this->field_name}[$langcode]);
266
    field_attach_update($entity_type, $entity);
267
    $count = db_select($this->table)
268
      ->countQuery()
269
      ->execute()
270
      ->fetchField();
271
    $this->assertEqual($count, 2, 'Missing field translation leaves data in table.');
272

    
273
    // Update: Field translation is NULL but field is not empty. Translation
274
    // data should be wiped.
275
    $entity->{$this->field_name}[$langcode] = NULL;
276
    field_attach_update($entity_type, $entity);
277
    $count = db_select($this->table)
278
      ->countQuery()
279
      ->execute()
280
      ->fetchField();
281
    $this->assertEqual($count, 1, 'NULL field translation is wiped.');
282
  }
283

    
284
  /**
285
   * Test trying to update a field with data.
286
   */
287
  function testUpdateFieldSchemaWithData() {
288
    // Create a decimal 5.2 field and add some data.
289
    $field = array('field_name' => 'decimal52', 'type' => 'number_decimal', 'settings' => array('precision' => 5, 'scale' => 2));
290
    $field = field_create_field($field);
291
    $instance = array('field_name' => 'decimal52', 'entity_type' => 'test_entity', 'bundle' => 'test_bundle');
292
    $instance = field_create_instance($instance);
293
    $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
294
    $entity->decimal52[LANGUAGE_NONE][0]['value'] = '1.235';
295
    field_attach_insert('test_entity', $entity);
296

    
297
    // Attempt to update the field in a way that would work without data.
298
    $field['settings']['scale'] = 3;
299
    try {
300
      field_update_field($field);
301
      $this->fail(t('Cannot update field schema with data.'));
302
    }
303
    catch (FieldException $e) {
304
      $this->pass(t('Cannot update field schema with data.'));
305
    }
306
  }
307

    
308
  /**
309
   * Test that failure to create fields is handled gracefully.
310
   */
311
  function testFieldUpdateFailure() {
312
    // Create a text field.
313
    $field = array('field_name' => 'test_text', 'type' => 'text', 'settings' => array('max_length' => 255));
314
    $field = field_create_field($field);
315

    
316
    // Attempt to update the field in a way that would break the storage.
317
    $prior_field = $field;
318
    $field['settings']['max_length'] = -1;
319
    try {
320
      field_update_field($field);
321
      $this->fail(t('Update succeeded.'));
322
    }
323
    catch (Exception $e) {
324
      $this->pass(t('Update properly failed.'));
325
    }
326

    
327
    // Ensure that the field tables are still there.
328
    foreach (_field_sql_storage_schema($prior_field) as $table_name => $table_info) {
329
      $this->assertTrue(db_table_exists($table_name), format_string('Table %table exists.', array('%table' => $table_name)));
330
    }
331
  }
332

    
333
  /**
334
   * Test adding and removing indexes while data is present.
335
   */
336
  function testFieldUpdateIndexesWithData() {
337

    
338
    // Create a decimal field.
339
    $field_name = 'testfield';
340
    $field = array('field_name' => $field_name, 'type' => 'text');
341
    $field = field_create_field($field);
342
    $instance = array('field_name' => $field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle');
343
    $instance = field_create_instance($instance);
344
    $tables = array(_field_sql_storage_tablename($field), _field_sql_storage_revision_tablename($field));
345

    
346
    // Verify the indexes we will create do not exist yet.
347
    foreach ($tables as $table) {
348
      $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value'), format_string("No index named value exists in %table", array('%table' => $table)));
349
      $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value_format'), format_string("No index named value_format exists in %table", array('%table' => $table)));
350
    }
351

    
352
    // Add data so the table cannot be dropped.
353
    $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
354
    $entity->{$field_name}[LANGUAGE_NONE][0]['value'] = 'field data';
355
    field_attach_insert('test_entity', $entity);
356

    
357
    // Add an index
358
    $field = array('field_name' => $field_name, 'indexes' => array('value' => array(array('value', 255))));
359
    field_update_field($field);
360
    foreach ($tables as $table) {
361
      $this->assertTrue(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value"), format_string("Index on value created in %table", array('%table' => $table)));
362
    }
363

    
364
    // Add a different index, removing the existing custom one.
365
    $field = array('field_name' => $field_name, 'indexes' => array('value_format' => array(array('value', 127), array('format', 127))));
366
    field_update_field($field);
367
    foreach ($tables as $table) {
368
      $this->assertTrue(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value_format"), format_string("Index on value_format created in %table", array('%table' => $table)));
369
      $this->assertFalse(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value"), format_string("Index on value removed in %table", array('%table' => $table)));
370
    }
371

    
372
    // Verify that the tables were not dropped.
373
    $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
374
    field_attach_load('test_entity', array(0 => $entity));
375
    $this->assertEqual($entity->{$field_name}[LANGUAGE_NONE][0]['value'], 'field data', "Index changes performed without dropping the tables");
376
  }
377

    
378
  /**
379
   * Test the storage details.
380
   */
381
  function testFieldStorageDetails() {
382
    $current = _field_sql_storage_tablename($this->field);
383
    $revision = _field_sql_storage_revision_tablename($this->field);
384

    
385
    // Retrieve the field and instance with field_info so the storage details are attached.
386
    $field = field_info_field($this->field['field_name']);
387
    $instance = field_info_instance($this->instance['entity_type'], $this->instance['field_name'], $this->instance['bundle']);
388

    
389
    // The storage details are indexed by a storage engine type.
390
    $this->assertTrue(array_key_exists('sql', $field['storage']['details']), 'The storage type is SQL.');
391

    
392
    // The SQL details are indexed by table name.
393
    $details = $field['storage']['details']['sql'];
394
    $this->assertTrue(array_key_exists($current, $details[FIELD_LOAD_CURRENT]), 'Table name is available in the instance array.');
395
    $this->assertTrue(array_key_exists($revision, $details[FIELD_LOAD_REVISION]), 'Revision table name is available in the instance array.');
396

    
397
    // Test current and revision storage details together because the columns
398
    // are the same.
399
    foreach ((array) $this->field['columns'] as $column_name => $attributes) {
400
      $storage_column_name = _field_sql_storage_columnname($this->field['field_name'], $column_name);
401
      $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)));
402
      $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)));
403
    }
404
  }
405

    
406
  /**
407
   * Test foreign key support.
408
   */
409
  function testFieldSqlStorageForeignKeys() {
410
    // Create a 'shape' field, with a configurable foreign key (see
411
    // field_test_field_schema()).
412
    $field_name = 'testfield';
413
    $foreign_key_name = 'shape';
414
    $field = array('field_name' => $field_name, 'type' => 'shape', 'settings' => array('foreign_key_name' => $foreign_key_name));
415
    field_create_field($field);
416

    
417
    // Retrieve the field definition and check that the foreign key is in place.
418
    $field = field_info_field($field_name);
419
    $this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name preserved through CRUD');
420
    $this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name preserved through CRUD');
421

    
422
    // Update the field settings, it should update the foreign key definition
423
    // too.
424
    $foreign_key_name = 'color';
425
    $field['settings']['foreign_key_name'] = $foreign_key_name;
426
    field_update_field($field);
427

    
428
    // Retrieve the field definition and check that the foreign key is in place.
429
    $field = field_info_field($field_name);
430
    $this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name modified after update');
431
    $this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name modified after update');
432

    
433
    // Now grab the SQL schema and verify that too.
434
    $schema = drupal_get_schema(_field_sql_storage_tablename($field), TRUE);
435
    $this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema');
436
    $foreign_key = reset($schema['foreign keys']);
437
    $foreign_key_column = _field_sql_storage_columnname($field['field_name'], $foreign_key_name);
438
    $this->assertEqual($foreign_key['table'], $foreign_key_name, 'Foreign key table name preserved in the schema');
439
    $this->assertEqual($foreign_key['columns'][$foreign_key_column], 'id', 'Foreign key column name preserved in the schema');
440
  }
441

    
442
  /**
443
   * Test handling multiple conditions on one column of a field.
444
   *
445
   * Tests both the result and the complexity of the query.
446
   */
447
  function testFieldSqlStorageMultipleConditionsSameColumn() {
448
    $entity = field_test_create_stub_entity(NULL, NULL);
449
    $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 1);
450
    field_test_entity_save($entity);
451

    
452
    $entity = field_test_create_stub_entity(NULL, NULL);
453
    $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 2);
454
    field_test_entity_save($entity);
455

    
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

    
460
    $query = new EntityFieldQuery();
461
    // This tag causes field_test_query_store_global_test_query_alter() to be
462
    // invoked so that the query can be tested.
463
    $query->addTag('store_global_test_query');
464
    $query->entityCondition('entity_type', 'test_entity');
465
    $query->entityCondition('bundle', 'test_bundle');
466
    $query->fieldCondition($this->field_name, 'value', 1, '<>', 0, LANGUAGE_NONE);
467
    $query->fieldCondition($this->field_name, 'value', 2, '<>', 0, LANGUAGE_NONE);
468
    $result = field_sql_storage_field_storage_query($query);
469

    
470
    // Test the results.
471
    $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
472

    
473
    // Test the complexity of the query.
474
    $query = $GLOBALS['test_query'];
475
    $this->assertNotNull($query, 'Precondition: the query should be available');
476
    $tables = $query->getTables();
477
    $this->assertEqual(1, count($tables), 'The query contains just one table.');
478

    
479
    // Clean up.
480
    unset($GLOBALS['test_query']);
481
  }
482

    
483
  /**
484
   * Test handling multiple conditions on multiple columns of one field.
485
   *
486
   * Tests both the result and the complexity of the query.
487
   */
488
  function testFieldSqlStorageMultipleConditionsDifferentColumns() {
489
    // Create the multi-column shape field
490
    $field_name = strtolower($this->randomName());
491
    $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4);
492
    $field = field_create_field($field);
493
    $instance = array(
494
      'field_name' => $field_name,
495
      'entity_type' => 'test_entity',
496
      'bundle' => 'test_bundle'
497
    );
498
    $instance = field_create_instance($instance);
499

    
500
    $entity = field_test_create_stub_entity(NULL, NULL);
501
    $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'X');
502
    field_test_entity_save($entity);
503

    
504
    $entity = field_test_create_stub_entity(NULL, NULL);
505
    $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'B', 'color' => 'X');
506
    field_test_entity_save($entity);
507

    
508
    $entity = field_test_create_stub_entity(NULL, NULL);
509
    $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'Y');
510
    field_test_entity_save($entity);
511

    
512
    $query = new EntityFieldQuery();
513
    // This tag causes field_test_query_store_global_test_query_alter() to be
514
    // invoked so that the query can be tested.
515
    $query->addTag('store_global_test_query');
516
    $query->entityCondition('entity_type', 'test_entity');
517
    $query->entityCondition('bundle', 'test_bundle');
518
    $query->fieldCondition($field_name, 'shape', 'B', '=', 'something', LANGUAGE_NONE);
519
    $query->fieldCondition($field_name, 'color', 'X', '=', 'something', LANGUAGE_NONE);
520
    $result = field_sql_storage_field_storage_query($query);
521

    
522
    // Test the results.
523
    $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
524

    
525
    // Test the complexity of the query.
526
    $query = $GLOBALS['test_query'];
527
    $this->assertNotNull($query, 'Precondition: the query should be available');
528
    $tables = $query->getTables();
529
    $this->assertEqual(1, count($tables), 'The query contains just one table.');
530

    
531
    // Clean up.
532
    unset($GLOBALS['test_query']);
533
  }
534

    
535
  /**
536
   * Test handling multiple conditions on multiple columns of one field for multiple languages.
537
   *
538
   * Tests both the result and the complexity of the query.
539
   */
540
  function testFieldSqlStorageMultipleConditionsDifferentColumnsMultipleLanguages() {
541
    field_test_entity_info_translatable('test_entity', TRUE);
542

    
543
    // Create the multi-column shape field
544
    $field_name = strtolower($this->randomName());
545
    $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4, 'translatable' => TRUE);
546
    $field = field_create_field($field);
547
    $instance = array(
548
      'field_name' => $field_name,
549
      'entity_type' => 'test_entity',
550
      'bundle' => 'test_bundle',
551
      'settings' => array(
552
        // Prevent warning from field_test_field_load().
553
        'test_hook_field_load' => FALSE,
554
      ),
555
    );
556
    $instance = field_create_instance($instance);
557

    
558
    $entity = field_test_create_stub_entity(NULL, NULL);
559
    $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'X');
560
    $entity->{$field_name}['en'][0] = array('shape' => 'B', 'color' => 'Y');
561
    field_test_entity_save($entity);
562
    $entity = field_test_entity_test_load($entity->ftid);
563

    
564
    $query = new EntityFieldQuery();
565
    // This tag causes field_test_query_store_global_test_query_alter() to be
566
    // invoked so that the query can be tested.
567
    $query->addTag('store_global_test_query');
568
    $query->entityCondition('entity_type', 'test_entity');
569
    $query->entityCondition('bundle', 'test_bundle');
570
    $query->fieldCondition($field_name, 'color', 'X', '=', NULL, LANGUAGE_NONE);
571
    $query->fieldCondition($field_name, 'shape', 'B', '=', NULL, 'en');
572
    $result = field_sql_storage_field_storage_query($query);
573

    
574
    // Test the results.
575
    $this->assertEqual(1, count($result), format_string('One result should be returned, got @count', array('@count' => count($result))));
576

    
577
    // Test the complexity of the query.
578
    $query = $GLOBALS['test_query'];
579
    $this->assertNotNull($query, 'Precondition: the query should be available');
580
    $tables = $query->getTables();
581
    $this->assertEqual(2, count($tables), 'The query contains two tables.');
582

    
583
    // Clean up.
584
    unset($GLOBALS['test_query']);
585
  }
586
}