source: sipes/modules_contrib/cck/tests/content.crud.test @ a8b1f3f

stableversion-3.0
Last change on this file since a8b1f3f was dafc8d8, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo el modulo

  • Propiedad mode establecida a 100755
File size: 52.2 KB
Línea 
1<?php
2
3// TODO:
4// - Test search indexing
5// - Test values reordering with preview and failed validation
6
7/**
8 * Base class for CCK CRUD tests.
9 * Defines many helper functions useful for writing CCK CRUD tests.
10 */
11class ContentCrudTestCase extends DrupalWebTestCase {
12  var $enabled_schema = FALSE;
13  var $content_types  = array();
14  var $nodes          = array();
15  var $last_field     = NULL;
16  var $next_field_n   = 1;
17
18  /**
19   * Enable CCK, Text, and Schema modules.
20   */
21  function setUp() {
22    $args = func_get_args();
23    $modules = array_merge(array('content_test', 'content', 'schema', 'text'), $args);
24    call_user_func_array(array('parent','setUp'), $modules);
25    module_load_include('inc', 'content', 'includes/content.crud');
26  }
27
28  // Database schema related helper functions
29
30  /**
31   * Checks that the database itself and the reported database schema match the
32   * expected columns for the given tables.
33   * @param $tables An array containing the key 'per_field' and/or the key 'per_type'.
34   *  These keys should have array values with table names as the keys (without the 'content_' / 'content_type_' prefix)
35   *  These keys should have either NULL value to indicate the table should be absent, or
36   *  array values containing column names. The column names can themselves be arrays, in
37   *  which case the contents of the array are treated as column names and prefixed with
38   *  the array key.
39   *
40   * For example, if called with the following as an argument:
41   * array(
42   *   'per_field' => array(
43   *     'st_f1' => array('delta', 'field_f1' => array('value, 'format')),
44   *     'st_f2' => NULL,    // no content_field_f2 table
45   *   ),
46   *   'per_type' => array(
47   *     'st_t1' => array('field_f2' => array('value'), 'field_f3' => array('value', 'format')),
48   *     'st_t2' => array(), // only 'nid' and 'vid' columns
49   *     'st_t3' => NULL,    // no content_type_t3 table
50   *   ),
51   * )
52   * Then the database and schema will be checked to ensure that:
53   *   content_st_f1 table contains fields nid, vid, delta, field_f1_value, field_f1_format
54   *   content_st_f2 table is absent
55   *   content_type_st_t1 table contains fields nid, vid, field_f2_value, field_f3_value, field_f3_format
56   *   content_type_st_t2 table contains fields nid, vid
57   *   content_type_st_t3 table is absent
58   */
59  function assertSchemaMatchesTables($tables) {
60    $groups = array('per_field' => 'content_', 'per_type' => 'content_type_');
61
62    foreach ($groups as $group => $table_prefix) {
63      if (isset($tables[$group])) {
64        foreach ($tables[$group] as $entity => $columns) {
65          if (isset($columns)) {
66            $db_columns = array('nid', 'vid');
67            foreach ($columns as $prefix => $items) {
68              if (is_array($items)) {
69                foreach ($items as $item) {
70                  $db_columns[] = $prefix .'_'. $item;
71                }
72              }
73              else {
74                $db_columns[] = $items;
75              }
76            }
77            $this->_assertSchemaMatches($table_prefix . $entity, $db_columns);
78          }
79          else {
80            $this->_assertTableNotExists($table_prefix . $entity);
81          }
82        }
83      }
84    }
85  }
86
87  /**
88   * Helper function for assertSchemaMatchesTables
89   * Checks that the given database table does NOT exist
90   * @param $table Name of the table to check
91   */
92  function _assertTableNotExists($table) {
93    $this->assertFalse(db_table_exists($table), t('Table !table is absent', array('!table' => $table)));
94  }
95
96  /**
97   * Helper function for assertSchemaMatchesTables
98   * Checks that the database and schema for the given table contain only the expected fields.
99   * @param $table Name of the table to check
100   * @param $columns Array of column names
101   */
102  function _assertSchemaMatches($table, $columns) {
103    // First test: check the expected structure matches the stored schema.
104    $schema = drupal_get_schema($table, TRUE);
105    $mismatches = array();
106    if ($schema === FALSE) {
107      $mismatches[] = t('table does not exist');
108    }
109    else {
110      $fields = $schema['fields'];
111      foreach ($columns as $field) {
112        if (!isset($fields[$field])) {
113          $mismatches[] = t('field !field is missing from table', array('!field' => $field));
114        }
115      }
116      $columns_reverse = array_flip($columns);
117      foreach ($fields as $name => $info) {
118        if(!isset($columns_reverse[$name])) {
119          $mismatches[] = t('table contains unexpected field !field', array('!field' => $name));
120        }
121      }
122    }
123    $this->assertEqual(count($mismatches), 0, t('Table !table matches schema: !details',
124      array('!table' => $table, '!details' => implode($mismatches, ', '))));
125
126    // Second test: check the schema matches the actual db structure.
127    // This is the part that relies on schema.module.
128    if (!$this->enabled_schema) {
129      $this->enabled_schema = module_exists('schema');
130    }
131    if ($this->enabled_schema) {
132      // Clunky workaround for http://drupal.org/node/215198
133      $prefixed_table = db_prefix_tables('{'. $table .'}');
134      $inspect = schema_invoke('inspect', $prefixed_table);
135      $inspect = isset($inspect[$table]) ? $inspect[$table] : NULL;
136      $compare = schema_compare_table($schema, $inspect);
137      if ($compare['status'] == 'missing') {
138        $compare['reasons'] = array(t('table does not exist'));
139      }
140    }
141    else {
142      $compare = array('status' => 'unknown', 'reasons' => array(t('cannot enable schema module')));
143    }
144    $this->assertEqual($compare['status'], 'same', t('Table schema for !table matches database: !details',
145      array('!table' => $table, '!details' => implode($compare['reasons'], ', '))));
146  }
147
148  // Node data helper functions
149
150  /**
151   * Helper function for assertNodeSaveValues. Recursively checks that
152   * all the keys of a table are present in a second and have the same value.
153   */
154  function _compareArrayForChanges($fields, $data, $message, $prefix = '') {
155    foreach ($fields as $key => $value) {
156      $newprefix = ($prefix == '') ? $key : $prefix .']['. $key;
157      if (is_array($value)) {
158        $compare_to = isset($data[$key]) ? $data[$key] : array();
159        $this->_compareArrayForChanges($value, $compare_to, $message, $newprefix);
160      }
161      else {
162        $this->assertEqual($value, $data[$key], t($message, array('!key' => $newprefix)));
163      }
164    }
165  }
166
167  /**
168   * Checks that after a node is saved using node_save, the values to be saved
169   * match up with the output from node_load.
170   * @param $node Either a node object, or the index of an acquired node
171   * @param $values Array of values to be merged with the node and passed to node_save
172   * @return The values array
173   */
174  function assertNodeSaveValues($node, $values) {
175    if (is_numeric($node) && isset($this->nodes[$node])) {
176      $node = $this->nodes[$node];
177    }
178    $node = $values + (array)$node;
179    $node = (object)$node;
180    node_save($node);
181    $this->assertNodeValues($node, $values);
182    return $values;
183  }
184
185  /**
186   * Checks that the output from node_load matches the expected values.
187   * @param $node Either a node object, or the index of an acquired node (only the nid field is used)
188   * @param $values Array of values to check against node_load. The node object must contain the keys in the array,
189   *  and the values must be equal, but the node object may also contain other keys.
190   */
191  function assertNodeValues($node, $values) {
192    if (is_numeric($node) && isset($this->nodes[$node])) {
193      $node = $this->nodes[$node];
194    }
195    $node = node_load($node->nid, NULL, TRUE);
196    $this->_compareArrayForChanges($values, (array)$node, 'Node data [!key] is correct');
197  }
198
199  /**
200   * Checks that the output from node_load is missing certain fields
201   * @param $node Either a node object, or the index of an acquired node (only the nid field is used)
202   * @param $fields Array containing a list of field names
203   */
204  function assertNodeMissingFields($node, $fields) {
205    if (is_numeric($node) && isset($this->nodes[$node])) {
206      $node = $this->nodes[$node];
207    }
208    $node = (array)node_load($node->nid, NULL, TRUE);
209    foreach ($fields as $field) {
210      $this->assertFalse(isset($node[$field]), t('Node should be lacking field !key', array('!key' => $field)));
211    }
212  }
213
214  /**
215   * Creates random values for a text field
216   * @return An array containing a value key and a format key
217   */
218  function createRandomTextFieldData() {
219    return array(
220      'value' => '!SimpleTest! test value' . $this->randomName(60),
221      'format' => 2,
222    );
223  }
224
225  // Login/user helper functions
226
227  /**
228   * Creates a user / role with certain permissions and then logs in as that user
229   * @param $permissions Array containing list of permissions. If not given, defaults to
230   *  access content, administer content types, administer nodes and administer filters.
231   */
232  function loginWithPermissions($permissions = NULL) {
233    if (!isset($permissions)) {
234      $permissions = array(
235        'access content',
236        'administer content types',
237        'administer nodes',
238        'administer filters',
239      );
240    }
241    $user = $this->drupalCreateUser($permissions);
242    $this->drupalLogin($user);
243  }
244
245  // Creation helper functions
246
247  /**
248   * Creates a number of content types with predictable names (simpletest_t1 ... simpletest_tN)
249   * These content types can later be accessed via $this->content_types[0 ... N-1]
250   * @param $count Number of content types to create
251   */
252  function acquireContentTypes($count) {
253    $this->content_types = array();
254    for ($i = 0; $i < $count; $i++) {
255      $name = 'simpletest_t'. ($i + 1);
256      $this->content_types[$i] = $this->drupalCreateContentType(array(
257        'name' => $name,
258        'type' => $name,
259      ));
260    }
261    content_clear_type_cache();
262  }
263
264  /**
265   * Creates a number of nodes of each acquired content type.
266   * Remember to call acquireContentTypes() before calling this, else the content types won't exist.
267   * @param $count Number of nodes to create per acquired content type (defaults to 1)
268   */
269  function acquireNodes($count = 1) {
270    $this->nodes = array();
271    foreach ($this->content_types as $content_type) {
272      for ($i = 0; $i < $count; $i++) {
273        $this->nodes[] = $this->drupalCreateNode(array('type' => $content_type->type));
274      }
275    }
276  }
277
278  /**
279   * Creates a field instance with a predictable name. Also makes all future calls to functions
280   * which take an optional field use this one as the default.
281   * @param $settings Array to be passed to content_field_instance_create. If the field_name
282   *  or type_name keys are missing, then they will be added. The default field name is
283   *  simpletest_fN, where N is 1 for the first created field, and increments. The default
284   *  type name is type name of the $content_type argument.
285   * @param $content_type Either a content type object, or the index of an acquired content type
286   * @return The newly created field instance.
287   */
288  function createField($settings, $content_type = 0) {
289    if (is_numeric($content_type) && isset($this->content_types[$content_type])) {
290      $content_type = $this->content_types[$content_type];
291    }
292    $defaults = array(
293      'field_name' => 'simpletest_f'. $this->next_field_n++,
294      'type_name' => $content_type->type,
295    );
296    $settings = $settings + $defaults;
297    $this->last_field = content_field_instance_create($settings);
298    return $this->last_field;
299  }
300
301  /**
302   * Creates a textfield instance. Identical to createField() except it ensures that the text module
303   * is enabled, and adds default settings of type (text) and widget_type (text_textfield) if they
304   * are not given in $settings.
305   * @sa createField()
306   */
307  function createFieldText($settings, $content_type = 0) {
308    $defaults = array(
309      'type' => 'text',
310      'widget_type' => 'text_textfield',
311    );
312    $settings = $settings + $defaults;
313    return $this->createField($settings, $content_type);
314  }
315
316  // Field manipulation helper functions
317
318  /**
319   * Updates a field instance. Also makes all future calls to functions which take an optional
320   * field use the updated one as the default.
321   * @param $settings New settings for the field instance. If the field_name or type_name keys
322   *  are missing, then they will be taken from $field.
323   * @param $field The field instance to update (defaults to the last worked upon field)
324   * @return The updated field instance.
325   */
326  function updateField($settings, $field = NULL) {
327    if (!isset($field)) {
328      $field = $this->last_field;
329    }
330    $defaults = array(
331      'field_name' => $field['field_name'],
332      'type_name'  => $field['type_name'] ,
333    );
334    $settings = $settings + $defaults;
335    $this->last_field = content_field_instance_update($settings);
336    return $this->last_field;
337  }
338
339  /**
340   * Makes a copy of a field instance on a different content type, effectively sharing the field with a new
341   * content type. Also makes all future calls to functions which take an optional field use the shared one
342   * as the default.
343   * @param $new_content_type Either a content type object, or the index of an acquired content type
344   * @param $field The field instance to share (defaults to the last worked upon field)
345   * @return The shared (newly created) field instance.
346   */
347  function shareField($new_content_type, $field = NULL) {
348    if (!isset($field)) {
349      $field = $this->last_field;
350    }
351    if (is_numeric($new_content_type) && isset($this->content_types[$new_content_type])) {
352      $new_content_type = $this->content_types[$new_content_type];
353    }
354    $field['type_name'] = $new_content_type->type;
355    $this->last_field = content_field_instance_create($field);
356    return $this->last_field;
357  }
358
359  /**
360   * Deletes an instance of a field.
361   * @param $content_type Either a content type object, or the index of an acquired content type (used only
362   *  to get field instance type name).
363   * @param $field The field instance to delete (defaults to the last worked upon field, used only to get
364   *  field instance field name).
365   */
366  function deleteField($content_type, $field = NULL) {
367    if (!isset($field)) {
368      $field = $this->last_field;
369    }
370    if (is_numeric($content_type) && isset($this->content_types[$content_type])) {
371      $content_type = $this->content_types[$content_type];
372    }
373    content_field_instance_delete($field['field_name'], $content_type->type);
374  }
375}
376
377class ContentCrudBasicTest extends ContentCrudTestCase {
378  public static function getInfo() {
379    return array(
380      'name' => t('CRUD - Basic API tests'),
381      'description' => t('Tests the field CRUD (create, read, update, delete) API. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array('@schema_link' => 'http://www.drupal.org/project/schema')),
382      'group' => t('CCK'),
383    );
384  }
385
386  function setUp() {
387    parent::setUp();
388    $this->acquireContentTypes(1);
389  }
390
391  function testBasic() {
392    // Create a field with both field and instance settings.
393    $field = $this->createFieldText(array('widget_type' => 'text_textarea', 'text_processing' => 1, 'rows' => 5), 0);
394
395
396    // Check that collapse and expand are inverse.
397    $fields = content_field_instance_read(array('field_name' => $field['field_name'], 'type_name' => $this->content_types[0]->type));
398    $field1 = array_pop($fields);
399
400    $field2 = content_field_instance_collapse($field1);
401    $field3 = content_field_instance_expand($field2);
402    $field4 = content_field_instance_collapse($field3);
403
404    $this->assertIdentical($field1, $field3, 'collapse then expand is identity');
405    $this->assertIdentical($field2, $field4, 'expand then collapse is identity');
406
407
408    // Check that collapse and expand are both final
409    // (e.g. do not further alter the data when called multiple times).
410    $fields = content_field_instance_read(array('field_name' => $field['field_name'], 'type_name' => $this->content_types[0]->type));
411    $field1 = array_pop($fields);
412
413    $field2 = content_field_instance_collapse($field1);
414    $field3 = content_field_instance_collapse($field2);
415    $this->assertIdentical($field2, $field3, 'collapse is final');
416
417    $field2 = content_field_instance_expand($field1);
418    $field3 = content_field_instance_expand($field2);
419    $this->assertIdentical($field2, $field3, 'expand is final');
420
421
422    // Check that updating a field as is leaves it unchanged.
423    $fields = content_field_instance_read(array('field_name' => $field['field_name'], 'type_name' => $this->content_types[0]->type));
424    $field1 = array_pop($fields);
425    $field2 = content_field_instance_update($field1);
426    $fields = content_field_instance_read(array('field_name' => $field['field_name'], 'type_name' => $this->content_types[0]->type));
427    $field3 = array_pop($fields);
428
429    $this->assertIdentical($field1, $field3, 'read, update, read is identity');
430  }
431}
432
433class ContentCrudSingleToMultipleTest extends ContentCrudTestCase {
434  public static function getInfo() {
435    return array(
436      'name' => t('CRUD - Single to multiple'),
437      'description' => t('Tests the field CRUD (create, read, update, delete) API by creating a single value field and changing it to a multivalue field, sharing it between several content types. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array('@schema_link' => 'http://www.drupal.org/project/schema')),
438      'group' => t('CCK'),
439    );
440  }
441
442  function setUp() {
443    parent::setUp();
444    $this->loginWithPermissions();
445    $this->acquireContentTypes(3);
446    $this->acquireNodes();
447  }
448
449  function testSingleToMultiple() {
450    // Create a simple text field
451    $this->createFieldText(array('text_processing' => 1));
452    $target_schema = array(
453      'per_type' => array(
454        'simpletest_t1' => array('simpletest_f1' => array('value', 'format'))
455      ),
456      'per_field' => array(),
457    );
458    $this->assertSchemaMatchesTables($target_schema);
459    $node0values = $this->assertNodeSaveValues(0, array(
460      'simpletest_f1' => array(
461        0 => $this->createRandomTextFieldData(),
462      )
463    ));
464
465    // Change the text field to allow multiple values
466    $this->updateField(array('multiple' => 1));
467    $target_schema = array(
468      'per_type' => array(
469        'simpletest_t1' => array(),
470      ),
471      'per_field' => array(
472        'simpletest_f1' => array('delta', 'simpletest_f1' => array('value', 'format')),
473      ),
474    );
475    $this->assertSchemaMatchesTables($target_schema);
476    $this->assertNodeValues(0, $node0values);
477
478    // Share the text field with 2 additional types t2 and t3.
479    for ($share_with_content_type = 1; $share_with_content_type <= 2; $share_with_content_type++) {
480      $this->shareField($share_with_content_type);
481      // There should be a new 'empty' per-type table for each content type that has fields.
482      $target_schema['per_type']['simpletest_t'. ($share_with_content_type + 1)] = array();
483      $this->assertSchemaMatchesTables($target_schema);
484      // The acquired node index will match the content type index as exactly one node is acquired per content type
485      $this->assertNodeSaveValues($share_with_content_type, array(
486        'simpletest_f1' => array(
487          0 => $this->createRandomTextFieldData(),
488        )
489      ));
490    }
491
492    // Delete the text field from all content types
493    for ($delete_from_content_type = 2; $delete_from_content_type >= 0; $delete_from_content_type--) {
494      $this->deleteField($delete_from_content_type);
495      // Content types that don't have fields any more shouldn't have any per-type table.
496      $target_schema['per_type']['simpletest_t'. ($delete_from_content_type + 1)] = NULL;
497      // After removing the last instance, there should be no table for the field either.
498      if ($delete_from_content_type == 0) {
499        $target_schema['per_field']['simpletest_f1'] = NULL;
500      }
501      $this->assertSchemaMatchesTables($target_schema);
502      // The acquired node index will match the content type index as exactly one node is acquired per content type
503      $this->assertNodeMissingFields($this->nodes[$delete_from_content_type], array('simpletest_f1'));
504    }
505  }
506}
507
508class ContentCrudMultipleToSingleTest extends ContentCrudTestCase {
509  public static function getInfo() {
510    return array(
511      'name' => t('CRUD - Multiple to single'),
512      'description' => t('Tests the field CRUD (create, read, update, delete) API by creating a multivalue field and changing it to a single value field, sharing it between several content types. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array('@schema_link' => 'http://www.drupal.org/project/schema')),
513      'group' => t('CCK'),
514    );
515  }
516
517  function setUp() {
518    parent::setUp();
519    $this->loginWithPermissions();
520    $this->acquireContentTypes(3);
521    $this->acquireNodes();
522  }
523
524  function testMultipleToSingle() {
525    // Create a multivalue text field
526    $this->createFieldText(array('text_processing' => 1, 'multiple' => 1));
527    $this->assertSchemaMatchesTables(array(
528      'per_type' => array(
529        'simpletest_t1' => array(),
530      ),
531      'per_field' => array(
532        'simpletest_f1' => array('delta', 'simpletest_f1' => array('value', 'format')),
533      ),
534    ));
535    $this->assertNodeSaveValues(0, array(
536      'simpletest_f1' => array(
537        0 => $this->createRandomTextFieldData(),
538        1 => $this->createRandomTextFieldData(),
539        2 => $this->createRandomTextFieldData(),
540      )
541    ));
542
543    // Change to a simple text field
544    $this->updateField(array('multiple' => 0));
545    $this->assertSchemaMatchesTables(array(
546      'per_type' => array(
547        'simpletest_t1' => array('simpletest_f1' => array('value', 'format')),
548      ),
549      'per_field' => array(
550        'simpletest_f1' => NULL,
551      ),
552    ));
553    $node0values = $this->assertNodeSaveValues(0, array(
554      'simpletest_f1' => array(
555        0 => $this->createRandomTextFieldData(),
556      )
557    ));
558
559    // Share the text field with other content type
560    $this->shareField(1);
561    $this->assertSchemaMatchesTables(array(
562      'per_type' => array(
563        'simpletest_t1' => array(),
564        'simpletest_t2' => array(),
565      ),
566      'per_field' => array(
567        'simpletest_f1' => array('simpletest_f1' => array('value', 'format')),
568      ),
569    ));
570    $node1values = $this->assertNodeSaveValues(1, array(
571      'simpletest_f1' => array(
572        0 => $this->createRandomTextFieldData(),
573      )
574    ));
575    $this->assertNodeValues(0, $node0values);
576
577    // Share the text field with a 3rd type
578    $this->shareField(2);
579    $this->assertSchemaMatchesTables(array(
580      'per_type' => array(
581        'simpletest_t1' => array(),
582        'simpletest_t2' => array(),
583        'simpletest_t3' => array(),
584      ),
585      'per_field' => array(
586        'simpletest_f1' => array('simpletest_f1' => array('value', 'format')),
587      ),
588    ));
589    $this->assertNodeSaveValues(2, array(
590      'simpletest_f1' => array(
591        0 => $this->createRandomTextFieldData(),
592      )
593    ));
594    $this->assertNodeValues(1, $node1values);
595    $this->assertNodeValues(0, $node0values);
596
597    // Remove text field from 3rd type
598    $this->deleteField(2);
599    $this->assertSchemaMatchesTables(array(
600      'per_type' => array(
601        'simpletest_t1' => array(),
602        'simpletest_t2' => array(),
603        'simpletest_t3' => NULL,
604      ),
605      'per_field' => array(
606        'simpletest_f1' => array('simpletest_f1' => array('value', 'format')),
607      ),
608    ));
609    $this->assertNodeMissingFields($this->nodes[2], array('simpletest_f1'));
610
611    // Remove text field from 2nd type (field isn't shared anymore)
612    $this->deleteField(1);
613    $this->assertSchemaMatchesTables(array(
614      'per_type' => array(
615        'simpletest_t1' => array('simpletest_f1' => array('value', 'format')),
616        'simpletest_t2' => NULL,
617        'simpletest_t3' => NULL,
618      ),
619      'per_field' => array(
620        'simpletest_f1' => NULL,
621      ),
622    ));
623    $this->assertNodeMissingFields(1, array('simpletest_f1'));
624    $this->assertNodeValues(0, $node0values);
625
626    // Remove text field from original type
627    $this->deleteField(0);
628    $this->assertSchemaMatchesTables(array(
629      'per_type' => array(
630        'simpletest_t1' => NULL,
631        'simpletest_t2' => NULL,
632        'simpletest_t3' => NULL,
633      ),
634      'per_field' => array(
635        'simpletest_f1' => NULL,
636      ),
637    ));
638    $this->assertNodeMissingFields(0, array('simpletest_f1'));
639  }
640}
641
642class ContentUICrud extends ContentCrudTestCase {
643  public static function getInfo() {
644    return array(
645      'name' => t('Admin UI'),
646      'description' => t('Tests the CRUD (create, read, update, delete) operations for content fields via the UI. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array('@schema_link' => 'http://www.drupal.org/project/schema')),
647      'group' => t('CCK'),
648    );
649  }
650
651  function setUp() {
652    parent::setUp('fieldgroup');
653    $this->loginWithPermissions();
654  }
655
656  function testAddFieldUI() {
657    // Add a content type with a random name (to avoid schema module problems).
658    $type1 = 'simpletest'. mt_rand();
659    $type1_name = $this->randomName(10);
660    $edit = array(
661      'type' => $type1,
662      'name' => $type1_name,
663    );
664    $this->drupalPost('admin/content/types/add', $edit, 'Save content type');
665    $admin_type1_url = 'admin/content/node-type/'. $type1;
666
667    // Create a text field via the UI.
668    $field_name = strtolower($this->randomName(10));
669    $field_label = $this->randomName(10);
670    $edit = array(
671      '_add_new_field[label]' => $field_label,
672      '_add_new_field[field_name]' => $field_name,
673      '_add_new_field[type]' => 'text',
674      '_add_new_field[widget_type]' => 'text_textfield',
675    );
676    $this->drupalPost($admin_type1_url .'/fields', $edit, 'Save');
677    $this->assertRaw('These settings apply only to the <em>'. $field_label .'</em> field', 'Field settings page displayed');
678    $this->assertRaw('Size of textfield', 'Field and widget types correct.');
679    $this->assertNoRaw('Change basic information', 'No basic information displayed');
680    $field_name = 'field_'. $field_name;
681
682    $edit = array();
683    // POST to the page without reloading.
684    $this->drupalPost(NULL, $edit, 'Save field settings');
685    $this->assertRaw('Added field <em>'. $field_label .'</em>.', 'Field settings saved');
686    $field_type1_url = $admin_type1_url .'/fields/'. $field_name;
687    $this->assertRaw($field_type1_url, 'Field displayed on overview.');
688
689    // Check the schema - the values should be in the per-type table.
690    $this->assertSchemaMatchesTables(array(
691      'per_type' => array(
692        $type1 => array($field_name => array('value')),
693      ),
694    ));
695
696
697    // Add a second content type.
698    $type2 = 'simpletest'. mt_rand();
699    $type2_name = $this->randomName(10);
700    $edit = array(
701      'type' => $type2,
702      'name' => $type2_name,
703    );
704    $this->drupalPost('admin/content/types/add', $edit, 'Save content type');
705    $admin_type2_url = 'admin/content/node-type/'. $type2;
706
707    // Add the same field to the second content type.
708    $edit = array(
709      '_add_existing_field[label]' => $field_label,
710      '_add_existing_field[field_name]' => $field_name,
711      '_add_existing_field[widget_type]' => 'text_textarea',
712    );
713    $this->drupalPost($admin_type2_url .'/fields', $edit, 'Save');
714    $this->assertRaw('These settings apply only to the <em>'. $field_label .'</em> field', 'Field settings page displayed');
715    $this->assertRaw('Rows', 'Field and widget types correct.');
716    $this->assertNoRaw('Change basic information', 'No basic information displayed');
717
718    $edit = array();
719    $this->drupalPost(NULL, $edit, 'Save field settings');
720    $this->assertRaw('Added field <em>'. $field_label .'</em>.', 'Field settings saved');
721    $field_type2_url = $admin_type2_url .'/fields/'. $field_name;
722    $this->assertRaw($field_type2_url, 'Field displayed on overview.');
723
724    // Check that a separate table is created for the shared field, and
725    // that it's values are no longer in the per-type tables.
726    $this->assertSchemaMatchesTables(array(
727      'per_field' => array(
728        $field_name => array($field_name => array('value')),
729      ),
730      'per_type' => array(
731        $type1 => array(),
732        $type2 => array(),
733      ),
734    ));
735
736
737    // Chancge the basic settings for this field.
738    $edit = array();
739    $this->drupalPost($field_type2_url, $edit, 'Change basic information');
740    $this->assertRaw('Edit basic information', 'Basic information form displayed');
741
742    $field_label2 = $this->randomName(10);
743    $edit = array(
744      'label' => $field_label2,
745      'widget_type' => 'text_textfield',
746    );
747    $this->drupalPost(NULL, $edit, 'Continue');
748    $this->assertRaw('These settings apply only to the <em>'. $field_label2 .'</em> field', 'Label changed');
749    $this->assertRaw('Size of textfield', 'Widget changed');
750
751    $edit = array();
752    // POST to the page without reloading.
753    $this->drupalPost(NULL, $edit, 'Save field settings');
754    $this->assertRaw('Saved field <em>'. $field_label2 .'</em>.', 'Field settings saved');
755
756
757    // Add a group to the second content type.
758    $group1_name = strtolower($this->randomName(10));
759    $group1_label = $this->randomName(10);
760    $edit = array(
761      '_add_new_group[label]' => $group1_label,
762      '_add_new_group[group_name]' => $group1_name,
763    );
764    $this->drupalPost($admin_type2_url .'/fields', $edit, 'Save');
765    $group1_name = 'group_'. $group1_name;
766    $this->assertRaw($admin_type2_url .'/groups/'. $group1_name, 'Group created');
767
768
769    // Remove the field from the second type.
770    $edit = array();
771    $this->drupalPost($field_type2_url .'/remove', $edit, 'Remove');
772    $this->assertRaw('Removed field <em>'. $field_label2 .'</em> from <em>'. $type2_name .'</em>', 'Field removed');
773    $this->assertNoRaw($field_type2_url, 'Field not displayed on overview.');
774
775    // Check the schema - the values should be in the per-type table.
776    $this->assertSchemaMatchesTables(array(
777      'per_type' => array(
778        $type1 => array($field_name => array('value')),
779      ),
780    ));
781
782    // Add a new field, an existing field, and a group in the same submit.
783    $field2_label = $this->randomName(10);
784    $field2_name = strtolower($this->randomName(10));
785    $group2_label = $this->randomName(10);
786    $group2_name = strtolower($this->randomName(10));
787    $edit = array(
788      '_add_new_field[label]' => $field2_label,
789      '_add_new_field[field_name]' => $field2_name,
790      '_add_new_field[type]' => 'text',
791      '_add_new_field[widget_type]' => 'text_textfield',
792      '_add_new_field[parent]' => $group1_name,
793      '_add_existing_field[label]' => $field_label,
794      '_add_existing_field[field_name]' => $field_name,
795      '_add_existing_field[widget_type]' => 'text_textarea',
796      '_add_existing_field[parent]' => '_add_new_group',
797      '_add_new_group[label]' => $group2_label,
798      '_add_new_group[group_name]' => $group2_name,
799    );
800    $this->drupalPost($admin_type2_url .'/fields', $edit, 'Save');
801    $this->assertRaw('These settings apply only to the <em>'. $field2_label .'</em> field', 'Field settings page for new field displayed');
802    // Submit new field settings
803    $edit = array();
804    $this->drupalPost(NULL, $edit, 'Save field settings');
805    $this->assertRaw('Added field <em>'. $field2_label .'</em>.', 'Field settings for new field saved');
806    $this->assertRaw('These settings apply only to the <em>'. $field_label .'</em> field', 'Field settings page for existing field displayed');
807    // Submit existing field settings
808    $edit = array();
809    $this->drupalPost(NULL, $edit, 'Save field settings');
810    $this->assertRaw('Added field <em>'. $field_label .'</em>.', 'Field settings for existing field saved');
811    $field2_name = 'field_'. $field2_name;
812    $field2_type2_url = $admin_type2_url .'/fields/'. $field2_name;
813    $this->assertRaw($field2_type2_url, 'New field displayed in overview');
814    $this->assertRaw($field_type2_url, 'Existing field displayed in overview');
815    $group2_name = 'group_'. $group2_name;
816    $this->assertRaw($admin_type2_url .'/groups/'. $group2_name, 'New group displayed in overview');
817
818    // Check Parenting
819    $groups = fieldgroup_groups($type2, FALSE, TRUE);
820    $this->assertTrue(isset($groups[$group1_name]['fields'][$field2_name]), 'New field in correct group');
821    $this->assertTrue(isset($groups[$group2_name]['fields'][$field_name]), 'Existing field in correct group');
822    $this->assertFieldByXPath('//select[@id="edit-'. strtr($field2_name, '_', '-') .'-parent"]//option[@selected]', $group1_name, 'Parenting for new field correct in overview');
823    $this->assertFieldByXPath('//select[@id="edit-'. strtr($field_name, '_', '-') .'-parent"]//option[@selected]', $group2_name, 'Parenting for existing field correct in overview');
824
825    // Check the schema : field1 is shared, field2 is in the per-type table.
826    $this->assertSchemaMatchesTables(array(
827      'per_field' => array(
828        $field_name => array($field_name => array('value')),
829      ),
830      'per_type' => array(
831        $type1 => array(),
832        $type2 => array($field2_name => array('value')),
833      ),
834    ));
835
836    // TODO : test validation failures...
837    // TODO : test ordering and extra fields...
838  }
839
840  function testFieldContentUI() {
841    // Create a content type with a field
842    $type1 = 'simpletest'. mt_rand();
843    $type1_obj = $this->drupalCreateContentType(array('type' => $type1));
844    $admin_type1_url = 'admin/content/node-type/'. $type1;
845    $field_name  = strtolower($this->randomName(10));
846    $field_url = 'field_'. $field_name;
847    $field = $this->createFieldText(array('text_processing' => 1, 'multiple' => 0, 'field_name' => $field_url), $type1_obj);
848
849    // Save a node with content in the text field
850    $edit = array();
851    $edit['title'] = $this->randomName(20);
852    $edit['body'] = $this->randomName(20);
853    $value = $this->randomName(20);
854    $edit[$field_url.'[0][value]'] = $value;
855    $this->drupalPost('node/add/'. $type1, $edit, 'Save');
856    $node = node_load(array('title' => $edit['title']));
857    $this->drupalGet('node/'. $node->nid);
858    $this->assertText($value, 'Textfield value saved and displayed');
859
860    // Alter the field to have unlimited values
861    $edit = array();
862    $edit['multiple']  = '1';
863    $this->drupalPost($admin_type1_url .'/fields/'. $field_url, $edit, 'Save field settings');
864
865    // Save a node with content in multiple text fields
866    $edit = array();
867    $edit['title'] = $this->randomName(20);
868    $edit['body'] = $this->randomName(20);
869    // Add more textfields (non-JS).
870    $this->drupalPost('node/add/'. $type1, $edit, "Add another item");
871    $this->drupalPost(NULL, $edit, "Add another item");
872    $value1 = $this->randomName(20);
873    $value2 = $this->randomName(20);
874    $value3 = $this->randomName(20);
875    $edit[$field_url.'[0][value]'] = $value1;
876    $edit[$field_url.'[1][value]'] = $value2;
877    $edit[$field_url.'[2][value]'] = $value3;
878
879    // This will fail if we don't have at least 3 textfields.
880    $this->drupalPost(NULL, $edit, 'Save');
881    $node = node_load(array('title' => $edit['title']));
882    $this->drupalGet('node/'. $node->nid);
883    $this->assertText($value3, '3rd textfield value saved and displayed');
884  }
885}
886
887class ContentOptionWidgetTest extends ContentCrudTestCase {
888  public static function getInfo() {
889    return array(
890      'name' => t('Option widgets'),
891      'description' => t('Tests the optionwidgets.'),
892      'group' => t('CCK'),
893    );
894  }
895
896  function setUp() {
897    parent::setUp('optionwidgets');
898    $this->loginWithPermissions();
899    $this->acquireContentTypes(1);
900  }
901
902  // TODO: test a number field with optionwidgets stores 0 correctly ?
903  // TODO: test the case where aliases and values overlap ? (http://drupal.org/node/281749)
904  // TODO: test noderef select widget...
905
906  /**
907   * On/Off Checkbox, not required:
908   * - Create a node with the value checked.
909   * - FAILS: Edit the node and uncheck the value.
910   *
911   * On/Off Checkbox, required:
912   * - TODO: what behavior do we want ?
913   */
914  function testOnOffCheckbox() {
915    $type = $this->content_types[0];
916    $type_url = str_replace('_', '-', $type->type);
917
918    // Create the field.
919    $on_text = $this->randomName(5);
920    $on_value = $this->randomName(5);
921    $off_text = $on_text. '_off';
922    $off_value = $on_value. '_off';
923
924    $settings = array(
925      'type' => 'text',
926      'widget_type' => 'optionwidgets_onoff',
927      'allowed_values' => "$off_value|$off_text\r\n$on_value|$on_text",
928    );
929    $field = $this->createField($settings, 0);
930    $field_name = $field['field_name'];
931
932    // Create a node with the checkbox on.
933    $edit = array(
934      'title' => $this->randomName(20),
935      'body' => $this->randomName(20),
936      $field_name.'[value]' => $on_value,
937    );
938    $this->drupalPost('node/add/'. $type_url, $edit, 'Save');
939    $node = node_load(array('title' => $edit['title']));
940    $this->assertEqual($node->{$field_name}[0]['value'], $on_value, 'Checkbox: checked (saved)');
941    $this->drupalGet('node/'. $node->nid);
942    $this->assertText($on_text, 'Checkbox: checked (displayed)');
943
944    // Edit the node and uncheck the box.
945    $edit = array(
946      $field_name.'[value]' => FALSE,
947    );
948    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
949    $node = node_load($node->nid, NULL, TRUE);
950    $this->assertEqual($node->{$field_name}[0]['value'], $off_value, 'Checkbox: unchecked (saved)');
951    $this->drupalGet('node/'. $node->nid);
952    $this->assertText($off_text, 'Checkbox: unchecked (displayed)');
953  }
954
955  /**
956   * Single select, not required:
957   * - TODO: check there's a 'none' choice in the form.
958   * - Create a node with one value selected.
959   * - Edit the node and unselect the value (selecting '- None -').
960   *
961   * Single select, required:
962   * - TODO: check there's no 'none' choice in the form.
963   *
964   * Multiple select, not required:
965   * - TODO: check there's a 'none' choice in the form.
966   * - Edit the node and select multiple values.
967   * - Edit the node and unselect one value.
968   * - Edit the node and unselect the values (selecting '- None -').
969   * - Edit the node and unselect the values (selecting nothing).
970   *
971   * Multiple select, required:
972   * - TODO: check there's no 'none' choice in the form.
973   * - Check the form doesn't submit when nothing is selected.
974   */
975  function testSelect() {
976    $type = $this->content_types[0];
977    $type_url = str_replace('_', '-', $type->type);
978
979    // Create the field - start with 'single'.
980    $value1 = $this->randomName(5);
981    $value1_alias = $value1 .'_alias';
982    $value2 = $this->randomName(5);
983    $value2_alias = $value2 .'_alias';
984
985    $settings = array(
986      'type' => 'text',
987      'widget_type' => 'optionwidgets_select',
988      'allowed_values' => "$value1|$value1_alias\r\n$value2|$value2_alias",
989    );
990    $field = $this->createField($settings, 0);
991    $field_name = $field['field_name'];
992
993    // Create a node with one value selected
994    $edit = array(
995      'title' => $this->randomName(20),
996      'body' => $this->randomName(20),
997    );
998    $edit[$field_name.'[value]'] = $value1;
999    $this->drupalPost('node/add/'. $type_url, $edit, 'Save');
1000    $node = node_load(array('title' => $edit['title']));
1001    $this->assertEqual($node->{$field_name}[0]['value'], $value1, 'Select: selected (saved)');
1002    $this->drupalGet('node/'. $node->nid);
1003    $this->assertText($value1_alias, 'Select: selected (displayed)');
1004
1005    // Edit the node and unselect the value (selecting '- None -').
1006    $edit = array(
1007      $field_name.'[value]' => '',
1008    );
1009    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1010    $node = node_load($node->nid, NULL, TRUE);
1011    $this->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Select: unselected (saved)');
1012    $this->drupalGet('node/'. $node->nid);
1013    $this->assertNoText($value1_alias, 'Select: unselected (displayed)');
1014
1015    // Change to a multiple field
1016    $field = $this->updateField(array('multiple' => '1', 'required' => '0'));
1017
1018    // Edit the node and select multiple values.
1019    $edit = array(
1020      $field_name.'[value][]' => array($value1 => $value1, $value2 => $value2),
1021    );
1022    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1023    $node = node_load($node->nid, NULL, TRUE);
1024    $this->assertEqual($node->{$field_name}[0]['value'], $value1, 'Multiple Select: selected 1 (saved)');
1025    $this->assertEqual($node->{$field_name}[1]['value'], $value2, 'Multiple Select: selected 2 (saved)');
1026    $this->drupalGet('node/'. $node->nid);
1027    $this->assertText($value1_alias, 'Multiple Select: selected 1 (displayed)');
1028    $this->assertText($value2_alias, 'Multiple Select: selected 2 (displayed)');
1029
1030    // Edit the node and unselect one value.
1031    $edit = array(
1032      $field_name.'[value][]' => array($value1 => $value1),
1033    );
1034    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1035    $node = node_load($node->nid, NULL, TRUE);
1036    $this->assertEqual($node->{$field_name}[0]['value'], $value1, 'Multiple Select: selected 1 (saved)');
1037    $this->assertTrue(!isset($node->{$field_name}[1]), 'Multiple Select: unselected 2 (saved)');
1038    $this->drupalGet('node/'. $node->nid);
1039    $this->assertText($value1_alias, 'Multiple Select: selected 1 (displayed)');
1040    $this->assertNoText($value2_alias, 'Multiple Select: unselected 2 (displayed)');
1041
1042    // Edit the node and unselect the values (selecting '- None -').
1043    $edit = array(
1044      $field_name.'[value][]' => array('' => ''),
1045    );
1046    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1047    $node = node_load($node->nid, NULL, TRUE);
1048    $this->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Multiple Select: unselected 1 ("-none-" selected) (saved)');
1049    $this->assertTrue(!isset($node->{$field_name}[1]), 'Multiple Select: unselected 2 ("-none-" selected) (saved)');
1050    $this->drupalGet('node/'. $node->nid);
1051    $this->assertNoText($value1_alias, 'Multiple Select: unselected 1 ("-none-" selected) (displayed)');
1052    $this->assertNoText($value2_alias, 'Multiple Select: unselected 2 ("-none-" selected) (displayed)');
1053
1054    // Edit the node and unselect the values (selecting nothing).
1055    // We first need to put values back in (no test needed).
1056    $edit = array();
1057    $edit[$field_name.'[value][]'] = array($value1 => FALSE, $value2 => FALSE);
1058    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1059    $edit = array();
1060    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1061    $node = node_load($node->nid, NULL, TRUE);
1062    $this->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Multiple Select: unselected 1 (no selection) (saved)');
1063    $this->assertTrue(!isset($node->{$field_name}[1]), 'Multiple Select: unselected 2 (no selection) (saved)');
1064    $this->drupalGet('node/'. $node->nid);
1065    $this->assertNoText($value1_alias, 'Multiple Select: unselected 1 (no selection) (displayed)');
1066    $this->assertNoText($value2_alias, 'Multiple Select: unselected 2 (no selection) (displayed)');
1067
1068    // Change the field to 'required'.
1069    $field = $this->updateField(array('required' => '1'));
1070
1071    // Check the form doesn't submit when nothing is selected.
1072    $edit = array();
1073    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1074    $this->assertRaw(t('!name field is required.', array('!name' => t($field['widget']['label']))), 'Multiple Select: "required" property is respected');
1075
1076    $edit = array(
1077      'title' => $this->randomName(20),
1078      'body' => $this->randomName(20),
1079    );
1080    $this->drupalPost('node/add/'. $type_url, $edit, 'Save');
1081    $this->assertRaw(t('!name field is required.', array('!name' => t($field['widget']['label']))), 'Multiple Select: "required" property is respected');
1082
1083  }
1084
1085  /**
1086   * Single (radios), not required:
1087   * - TODO: check there's a 'none' choice in the form.
1088   * - Create a node with one value selected.
1089   * - Edit the node and unselect the value (selecting '- None -').
1090   *
1091   * Single (radios), required:
1092   * - TODO: check there's no 'none' choice in the form.
1093   * - Check the form doesn't submit when nothing is selected.
1094   */
1095  function testRadios() {
1096    $type = $this->content_types[0];
1097    $type_url = str_replace('_', '-', $type->type);
1098
1099    // Create the field - 'single' (radios).
1100    $value1 = $this->randomName(5);
1101    $value1_alias = $value1 .'_alias';
1102    $value2 = $this->randomName(5);
1103    $value2_alias = $value2 .'_alias';
1104    $settings = array(
1105      'type' => 'text',
1106      'widget_type' => 'optionwidgets_buttons',
1107      'allowed_values' => "$value1|$value1_alias\r\n$value2|$value2_alias",
1108    );
1109    $field = $this->createField($settings, 0);
1110    $field_name = $field['field_name'];
1111
1112    // Create a node with one value selected
1113    $edit = array();
1114    $edit['title'] = $this->randomName(20);
1115    $edit['body'] = $this->randomName(20);
1116    $edit[$field_name.'[value]'] = $value1;
1117    $this->drupalPost('node/add/'. $type_url, $edit, 'Save');
1118    $node = node_load(array('title' => $edit['title']));
1119    $this->assertEqual($node->{$field_name}[0]['value'], $value1, 'Radios: checked (saved)');
1120    $this->drupalGet('node/'. $node->nid);
1121    $this->assertText($value1_alias, 'Radios: checked (displayed)');
1122
1123    // Edit the node and unselect the value (selecting '- None -').
1124    $edit = array();
1125    $edit[$field_name.'[value]'] = '';
1126    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1127    $node = node_load($node->nid, NULL, TRUE);
1128    $this->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Radios: unchecked (saved)');
1129    $this->drupalGet('node/'. $node->nid);
1130    $this->assertNoText($value1_alias, 'Radios: unchecked (displayed)');
1131
1132    // Change field to required.
1133    $field = $this->updateField(array('required' => '1'));
1134
1135    // Check the form doesn't submit when nothing is selected.
1136    // Doing this on the pre-filled node doesn't take, so we test that on a new node.
1137    $edit = array();
1138    $edit['title'] = $this->randomName(20);
1139    $edit['body'] = $this->randomName(20);
1140    $this->drupalPost('node/add/'. $type_url, $edit, 'Save');
1141    $this->assertRaw(t('!name field is required.', array('!name' => t($field['widget']['label']))), 'Radios: "required" property is respected');
1142  }
1143
1144  /**
1145   * Multiple (checkboxes), not required:
1146   * - TODO: check there's no 'none' choice in the form.
1147   * - Create a node with two values.
1148   * - Edit the node and select only one value.
1149   * - Edit the node and unselect the values (selecting nothing).
1150   *
1151   * Multiple (checkboxes), required:
1152   * - TODO: check there's no 'none' choice in the form.
1153   * - Check the form doesn't submit when nothing is selected.
1154   */
1155  function testChecboxes() {
1156    $type = $this->content_types[0];
1157    $type_url = str_replace('_', '-', $type->type);
1158
1159    // Create the field -  'multiple' (checkboxes).
1160    $value1 = $this->randomName(5);
1161    $value1_alias = $value1 .'_alias';
1162    $value2 = $this->randomName(5);
1163    $value2_alias = $value2 .'_alias';
1164    $settings = array(
1165      'type' => 'text',
1166      'multiple' => '1',
1167      'widget_type' => 'optionwidgets_buttons',
1168      'allowed_values' => "$value1|$value1_alias\r\n$value2|$value2_alias",
1169    );
1170    $field = $this->createField($settings, 0);
1171    $field_name = $field['field_name'];
1172
1173    // Create a node with two values selected
1174    $edit = array(
1175      'title' => $this->randomName(20),
1176      'body' => $this->randomName(20),
1177      $field_name.'[value]['. $value1 .']' => $value1,
1178      $field_name.'[value]['. $value2 .']' => $value2,
1179    );
1180    $this->drupalPost('node/add/'. $type_url, $edit, 'Save');
1181    $node = node_load(array('title' => $edit['title']));
1182    $this->assertEqual($node->{$field_name}[0]['value'], $value1, 'Checkboxes: selected 1 (saved)');
1183    $this->assertEqual($node->{$field_name}[1]['value'], $value2, 'Checkboxes: selected 2 (saved)');
1184    $this->drupalGet('node/'. $node->nid);
1185    $this->assertText($value1_alias, 'Checkboxes: selected 1 (displayed)');
1186    $this->assertText($value2_alias, 'Checkboxes: selected 2 (displayed)');
1187
1188    // Edit the node and unselect the values (selecting nothing -
1189    // there is no 'none' choice for checkboxes).
1190    $edit = array(
1191      $field_name.'[value]['. $value1 .']' => $value1,
1192      $field_name.'[value]['. $value2 .']' => FALSE,
1193    );
1194    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1195    $node = node_load($node->nid, NULL, TRUE);
1196    $this->assertEqual($node->{$field_name}[0]['value'], $value1, 'Checkboxes: selected 1 (saved)');
1197    $this->assertTrue(!isset($node->{$field_name}[1]), 'Checkboxes: unselected 2 (saved)');
1198    $this->drupalGet('node/'. $node->nid);
1199    $this->assertText($value1_alias, 'Checkboxes: selected 1 (displayed)');
1200    $this->assertNoText($value2_alias, 'Checkboxes: unselected 2 (displayed)');
1201
1202    // Edit the node and unselect the values (selecting nothing -
1203    // there is no 'none' choice for checkboxes).
1204    $edit = array(
1205      $field_name.'[value]['. $value1 .']' => FALSE,
1206      $field_name.'[value]['. $value2 .']' => FALSE,
1207    );
1208    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1209    $node = node_load($node->nid, NULL, TRUE);
1210    $this->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Checkboxes: unselected 1 (no selection) (saved)');
1211    $this->assertTrue(!isset($node->{$field_name}[1]), 'Checkboxes: unselected 2 (no selection) (saved)');
1212    $this->drupalGet('node/'. $node->nid);
1213    $this->assertNoText($value1_alias, 'Checkboxes: unselected 1 (no selection) (displayed)');
1214    $this->assertNoText($value2_alias, 'Checkboxes: unselected 2 (no selection) (displayed)');
1215
1216    // Change field to required.
1217    $field = $this->updateField(array('required' => '1'));
1218
1219    // Check the form doesn't submit when nothing is selected.
1220    $edit = array(
1221      $field_name.'[value]['. $value1 .']' => FALSE,
1222      $field_name.'[value]['. $value2 .']' => FALSE,
1223    );
1224    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
1225    $this->assertRaw(t('!name field is required.', array('!name' => t($field['widget']['label']))), 'Checkboxes: "required" property is respected');
1226
1227    $edit = array();
1228    $edit['title'] = $this->randomName(20);
1229    $edit['body'] = $this->randomName(20);
1230    $this->drupalPost('node/add/'. $type_url, $edit, 'Save');
1231    $this->assertRaw(t('!name field is required.', array('!name' => t($field['widget']['label']))), 'Checkboxes: "required" property is respected');
1232  }
1233
1234}
1235
1236class ContentEmptyDeltaTest extends ContentCrudTestCase {
1237  public static function getInfo() {
1238    return array(
1239      'name' => t('Empty deltas'),
1240      'description' => t('Test leaving empty values on a multivalue field and then removing them.'),
1241      'group' => t('CCK'),
1242    );
1243  }
1244
1245  function setUp() {
1246    parent::setUp();
1247    $this->loginWithPermissions();
1248    $this->acquireContentTypes(1);
1249  }
1250
1251  function testEmptyTextField() {
1252    // Create a content type with a multivalue text field.
1253    $type = $this->content_types[0];
1254    $type_url = str_replace('_', '-', $type->type);
1255    $value1 = $this->randomName(5);
1256    $value2 = $this->randomName(5);
1257    $value3 = $this->randomName(5);
1258    $field = $this->createFieldText(array('text_processing' => 0, 'multiple' => 1));
1259    $field_name = $field['field_name'];
1260
1261    // Create a node with three values set.
1262    $edit = array(
1263      'title' => $this->randomName(20),
1264      'body' => $this->randomName(20),
1265      'type' => $type->name,
1266    );
1267    $edit[$field_name][0]['value'] = $value1;
1268    $edit[$field_name][1]['value'] = $value2;
1269    $edit[$field_name][2]['value'] = $value3;
1270    $node = $this->drupalCreateNode($edit);
1271    $max_delta = max(array_keys($node->{$field_name}));
1272    $this->assertEqual($max_delta, 2, 'Three values saved, highest delta is 2');
1273    $this->drupalGet('node/'. $node->nid);
1274    $this->assertText($value1, 'First value displayed');
1275    $this->assertText($value2, 'Second value displayed');
1276    $this->assertText($value3, 'Third value displayed');
1277
1278    // Set second value to an empty string.
1279    $node->{$field_name}[1]['value'] = '';
1280    node_save($node);
1281    $node = node_load($node->nid, NULL, TRUE);
1282    $this->assertIdentical($node->{$field_name}[1]['value'], NULL, 'Second value is empty');
1283    $max_delta = max(array_keys($node->{$field_name}));
1284    $this->assertEqual($max_delta, 2, 'Three values saved, highest delta is 2');
1285    $this->drupalGet('node/'. $node->nid);
1286    $this->assertText($value1, 'First value displayed');
1287    $this->assertNoText($value2, 'Second value not displayed');
1288    $this->assertText($value3, 'Third value displayed');
1289
1290    // Remove the second value.
1291    $node->{$field_name}[1]['_remove'] = 1;
1292    node_save($node);
1293    $node = node_load($node->nid, NULL, TRUE);
1294    $this->assertEqual($node->{$field_name}[1]['value'], $value3, 'Third value has moved to delta 1');
1295    $max_delta = max(array_keys($node->{$field_name}));
1296    $this->assertEqual($max_delta, 1, 'Two values saved, highest delta is 1');
1297    $this->drupalGet('node/'. $node->nid);
1298    $this->assertText($value1, 'First value displayed');
1299    $this->assertNoText($value2, 'Second value not displayed');
1300    $this->assertText($value3, 'Third value displayed');
1301  }
1302}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.