Projet

Général

Profil

Paste
Télécharger (26,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / field / modules / field_sql_storage / field_sql_storage.test @ 01dfd3b5

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. The
317
    // parenthesis suffix is needed because SQLite has *very* relaxed rules for
318
    // data types, so we actually need to provide an invalid SQL syntax in order
319
    // to break it.
320
    // @see https://www.sqlite.org/datatype3.html
321
    $prior_field = $field;
322
    $field['settings']['max_length'] = '-1)';
323
    try {
324
      field_update_field($field);
325
      $this->fail(t('Update succeeded.'));
326
    }
327
    catch (Exception $e) {
328
      $this->pass(t('Update properly failed.'));
329
    }
330

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

    
337
  /**
338
   * Test adding and removing indexes while data is present.
339
   */
340
  function testFieldUpdateIndexesWithData() {
341

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

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

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

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

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

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

    
382
  /**
383
   * Test the storage details.
384
   */
385
  function testFieldStorageDetails() {
386
    $current = _field_sql_storage_tablename($this->field);
387
    $revision = _field_sql_storage_revision_tablename($this->field);
388

    
389
    // Retrieve the field and instance with field_info so the storage details are attached.
390
    $field = field_info_field($this->field['field_name']);
391
    $instance = field_info_instance($this->instance['entity_type'], $this->instance['field_name'], $this->instance['bundle']);
392

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

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

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

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

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

    
426
    // Update the field settings, it should update the foreign key definition
427
    // too.
428
    $foreign_key_name = 'color';
429
    $field['settings']['foreign_key_name'] = $foreign_key_name;
430
    field_update_field($field);
431

    
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

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

    
446
  /**
447
   * Test handling multiple conditions on one column of a field.
448
   *
449
   * Tests both the result and the complexity of the query.
450
   */
451
  function testFieldSqlStorageMultipleConditionsSameColumn() {
452
    $entity = field_test_create_stub_entity(NULL, NULL);
453
    $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 1);
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' => 2);
458
    field_test_entity_save($entity);
459

    
460
    $entity = field_test_create_stub_entity(NULL, NULL);
461
    $entity->{$this->field_name}[LANGUAGE_NONE][0] = array('value' => 3);
462
    field_test_entity_save($entity);
463

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

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

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

    
483
    // Clean up.
484
    unset($GLOBALS['test_query']);
485
  }
486

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

    
504
    $entity = field_test_create_stub_entity(NULL, NULL);
505
    $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', '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' => 'B', 'color' => 'X');
510
    field_test_entity_save($entity);
511

    
512
    $entity = field_test_create_stub_entity(NULL, NULL);
513
    $entity->{$field_name}[LANGUAGE_NONE][0] = array('shape' => 'A', 'color' => 'Y');
514
    field_test_entity_save($entity);
515

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

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

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

    
535
    // Clean up.
536
    unset($GLOBALS['test_query']);
537
  }
538

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

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

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

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

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

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

    
587
    // Clean up.
588
    unset($GLOBALS['test_query']);
589
  }
590
}