You are here

public function DoubleFieldFormattersTestCase::testTextFormats in Double Field 7.2

Test that content is filtered with appropriate format.

File

tests/double_field_formatters.test, line 92
Tests for Double field formatters.

Class

DoubleFieldFormattersTestCase
Test case for Double field formatters.

Code

public function testTextFormats() {

  // Unlock node settings.
  node_delete(1);
  $this->field['settings'] = DoubleFieldField::getFieldSettings('varchar_&_varchar');
  field_update_field($this->field);

  // Create a node with actual data for the field.
  $widget = new DoubleFieldWidget('textfield_&_textfield', $this->field['settings']);
  $edit = array(
    'type' => $this->type_name,
    'uid' => $this->user->uid,
  );
  $edit[$this->field_name][LANGUAGE_NONE][0] = $widget
    ->getValue();
  $test_node = $this
    ->drupalCreateNode($edit);
  $this
    ->drupalGet("admin/structure/types/manage/{$this->type_name}/display");
  $formatter = new DoubleFieldFormatter('double_field_unformatted_list');
  $edit = array(
    "fields[{$this->field_name}][type]" => $formatter
      ->getType(),
    'refresh_rows' => $this->field_name,
  );
  $this
    ->drupalPostAJAX(NULL, $edit, array(
    'op' => t('Refresh'),
  ));
  $this
    ->drupalPostAJAX(NULL, array(), $this->field_name . '_formatter_settings_edit');
  $input = $formatter
    ->getSettingsFormInput($this->field_name);
  $input["fields[{$this->field_name}][settings_edit_form][settings][style]"] = 'block';
  $input["fields[{$this->field_name}][settings_edit_form][settings][first][format]"] = 'full_html';
  $this
    ->drupalPostAJAX(NULL, $input, array(
    $this->field_name . '_formatter_settings_update' => t('Update'),
  ));
  $this
    ->drupalPostAJAX(NULL, array(), $this->field_name . '_formatter_settings_edit');
  $this
    ->verbose($this
    ->drupalGetContent());

  // Verify that full HTML format is chosen for first subfield.
  $this
    ->assertTrue($this
    ->xpath('//select[@name=:name]//option[@selected and  @value="full_html" and text()=:label]', array(
    ':name' => "fields[{$this->field_name}][settings_edit_form][settings][first][format]",
    ':label' => t('Full HTML'),
  )), 'Full HTML format is selected..');
  $this
    ->drupalPost(NULL, array(), t('Save'));
  $this
    ->assertText(t('Using the "Full HTML" format allows HTML to be posted unfiltered. This could represent a severe security risk.'), 'Warning message was found.');
  $this
    ->assertTrue($this
    ->xpath("//span[@style='color: red']//em[text()=:text]", array(
    ':text' => 'Full HTML',
  )), 'Full HTML format was highlighted');

  // Add some unsecure text to the node.
  $text = '<em>text</em><span style="color: red">text</span>';
  $test_node->{$this->field_name}[LANGUAGE_NONE][0] = array(
    'first' => $text,
    'second' => $text,
  );
  node_save($test_node);
  $this
    ->drupalGet('node/' . $test_node->nid);
  $em_xpath = "//div[@class='double-field-first']//em";
  $span_xpath = "//div[@class='double-field-first']//span";
  $this
    ->assertTrue($this
    ->xpath($em_xpath) && $this
    ->xpath($span_xpath), 'The text is filtered with full HTML format.');
  $this->instance['display']['default']['settings']['first']['format'] = 'filtered_html';
  field_update_instance($this->instance);
  $this
    ->drupalGet('node/' . $test_node->nid);
  $this
    ->assertTrue($this
    ->xpath($em_xpath) && !$this
    ->xpath($span_xpath), 'The text is filtered with filtered HTML format.');
  $this->instance['display']['default']['settings']['first']['format'] = 'plain_text';
  field_update_instance($this->instance);
  $this
    ->drupalGet('node/' . $test_node->nid);
  $this
    ->assertTrue(!$this
    ->xpath($em_xpath) && !$this
    ->xpath($span_xpath), 'The text is filtered with plain HTML format.');
  $this->instance['display']['default']['settings']['first']['format'] = '_none';
  field_update_instance($this->instance);
  $this
    ->drupalGet('node/' . $test_node->nid);
  $this
    ->assertTrue(!$this
    ->xpath($em_xpath) && !$this
    ->xpath($span_xpath), 'The text is filtered with check_plain() function.');
}