You are here

public function ParagraphsWebTestCase::testRequiredFieldInBundle in Paragraphs 7

Tests required field validation on paragraph bundle.

File

tests/paragraphs.test, line 91

Class

ParagraphsWebTestCase
Defines tests for paragraphs.

Code

public function testRequiredFieldInBundle() {
  $this
    ->drupalLogin($this->privilegedUser);
  $this
    ->drupalGet('node/add/paragraph-test');

  // Add a new paragraph before saving node.
  $this
    ->drupalPost(NULL, array(), t('Add new Paragraph'));
  $title = $this
    ->randomString(20);
  $create_edit = array(
    'title' => $title,
  );

  // Click the Save button to test whole-form validation.
  $this
    ->drupalPost(NULL, $create_edit, t('Save'));

  // Empty field should fail validation.
  $this
    ->assertRaw(t('!field field is required.', array(
    '!field' => 'PTest Text',
  )), 'Field failed whole-form validation');

  // Click the Collapse button to test per-paragraph validation.
  $this
    ->drupalPost(NULL, $create_edit, t('Collapse'));

  // Empty field should fail validation.
  $this
    ->assertRaw(t('!field field is required.', array(
    '!field' => 'PTest Text',
  )), 'Field failed per-paragraph validation');

  // Add value to field.
  $value1 = $this
    ->randomString(20);
  $create_edit = array(
    'field_paragraphs[und][0][field_ptest_text][und][0][value]' => $value1,
  );

  // Click Collapse button to close paragraph (should now pass).
  $this
    ->drupalPost(NULL, $create_edit, t('Collapse'));

  // Paragraph item should collapse after passing validation.
  $this
    ->assertRaw(t('Warning: this content must be saved to reflect changes on this paragraphs item.'), 'Field passed per-paragraph validation');
  $create_edit = array(
    'title' => $title,
  );

  // Save whole node.
  $this
    ->drupalPost(NULL, $create_edit, t('Save'));

  // Node should save.
  $this
    ->assertRaw(t('!post %title has been created.', array(
    '!post' => 'Paragraph Test',
    '%title' => $title,
  )), 'Paragraph test node created.');
  $this
    ->assertText(check_plain($value1), 'Value of paragraph was rendered.');
}