You are here

public function EntityMetadataIntegrationTestCase::testTextFields in Entity API 7

Test making use of a text fields.

File

./entity.test, line 1901
Entity CRUD API tests.

Class

EntityMetadataIntegrationTestCase
Tests provided entity property info of the core modules.

Code

public function testTextFields() {

  // Create a simple text field without text processing.
  $field = array(
    'field_name' => 'field_text',
    'type' => 'text',
    'cardinality' => 2,
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_text',
    'entity_type' => 'node',
    'label' => 'test',
    'bundle' => 'article',
    'widget' => array(
      'type' => 'text_textfield',
      'weight' => -1,
    ),
  );
  field_create_instance($instance);
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper->field_text[0] = 'the text';

  // Try saving the node and make sure the information is still there after
  // loading the node again, thus the correct data structure has been written.
  node_save($node);
  $node = node_load($node->nid, NULL, TRUE);
  $wrapper = entity_metadata_wrapper('node', $node);
  $this
    ->assertEqual('the text', $wrapper->field_text[0]
    ->value(), 'Text has been specified.');

  // Now activate text processing.
  $instance['settings']['text_processing'] = 1;
  field_update_instance($instance);
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper->field_text[0]
    ->set(array(
    'value' => "<b>The second body.</b>",
  ));
  $this
    ->assertEqual("<p>The second body.</p>\n", $wrapper->field_text[0]->value
    ->value(), "Setting a processed text field value and reading it again.");

  // Assert the summary property is correctly removed.
  $this
    ->assertFalse(isset($wrapper->field_text[0]->summary), 'Processed text has no summary.');

  // Create a text field with summary but without text processing.
  $field = array(
    'field_name' => 'field_text2',
    'type' => 'text_with_summary',
    'cardinality' => 1,
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_text2',
    'entity_type' => 'node',
    'label' => 'test',
    'bundle' => 'article',
    'settings' => array(
      'text_processing' => 0,
    ),
    'widget' => array(
      'type' => 'text_textarea_with_summary',
      'weight' => -1,
    ),
  );
  field_create_instance($instance);
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper->field_text2->summary = 'the summary';
  $wrapper->field_text2->value = 'the text';

  // Try saving the node and make sure the information is still there after
  // loading the node again, thus the correct data structure has been written.
  node_save($node);
  $node = node_load($node->nid, NULL, TRUE);
  $wrapper = entity_metadata_wrapper('node', $node);
  $this
    ->assertEqual('the text', $wrapper->field_text2->value
    ->value(), 'Text has been specified.');
  $this
    ->assertEqual('the summary', $wrapper->field_text2->summary
    ->value(), 'Summary has been specified.');
}