You are here

public function FieldCollectionBasicTestCase::testBasicUI in Field collection 7

Make sure the basic UI and access checks are working.

File

./field_collection.test, line 314
Tests for field_collections.

Class

FieldCollectionBasicTestCase
Test basics.

Code

public function testBasicUI() {

  // Add a field to the collection.
  $field = array(
    'field_name' => 'field_text',
    'type' => 'text',
    'cardinality' => 1,
    'translatable' => FALSE,
  );
  field_create_field($field);
  $instance = array(
    'entity_type' => 'field_collection_item',
    'field_name' => 'field_text',
    'bundle' => $this->field_name,
    'label' => 'Test text field',
    'widget' => array(
      'type' => 'text_textfield',
    ),
  );
  field_create_instance($instance);
  $user = $this
    ->drupalCreateUser();
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $this
    ->drupalLogin($user);

  // Make sure access is denied.
  $path = 'field-collection/field-test-collection/add/node/' . $node->nid;
  $this
    ->drupalGet($path);
  $this
    ->assertText(t('Access denied'), 'Access has been denied.');
  $user_privileged = $this
    ->drupalCreateUser(array(
    'access content',
    'edit any article content',
  ));
  $this
    ->drupalLogin($user_privileged);
  $this
    ->drupalGet("node/{$node->nid}");
  $this
    ->assertLinkByHref($path, 0, 'Add link is shown.');
  $this
    ->drupalGet($path);
  $this
    ->assertText(t('Test text field'), 'Add form is shown.');
  $edit['field_text[und][0][value]'] = self::randomName();
  $this
    ->drupalPost($path, $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'), 'Field collection saved.');
  $this
    ->assertText($edit['field_text[und][0][value]'], 'Added field value is shown.');
  $edit['field_text[und][0][value]'] = self::randomName();
  $this
    ->drupalPost('field-collection/field-test-collection/1/edit', $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'), 'Field collection saved.');
  $this
    ->assertText($edit['field_text[und][0][value]'], 'Field collection has been edited.');
  $this
    ->drupalGet('field-collection/field-test-collection/1');
  $this
    ->assertText($edit['field_text[und][0][value]'], 'Field collection can be viewed.');

  // Add further 3 items, so we have reached 4 == maxium cardinality.
  $this
    ->drupalPost($path, $edit, t('Save'));
  $this
    ->drupalPost($path, $edit, t('Save'));
  $this
    ->drupalPost($path, $edit, t('Save'));

  // Make sure adding doesn't work any more as we have restricted cardinality
  // to 1.
  $this
    ->drupalGet($path);
  $this
    ->assertResponse(403);
  $this
    ->drupalPost('field-collection/field-test-collection/1/delete', array(), t('Delete'));
  $this
    ->drupalGet($path);

  // Add form is shown again.
  $this
    ->assertText(t('Test text field'), 'Field collection item has been deleted.');

  // Test the viewing a revision. There should be no links to change it.
  $vid = $node->vid;
  $node = node_load($node->nid, NULL, TRUE);
  $node->revision = TRUE;
  node_save($node);
  $this
    ->drupalGet("node/{$node->nid}/revisions/{$vid}/view");
  $this
    ->assertResponse(403, 'Access to view revision denied');

  // Login in as admin and try again.
  $user = $this
    ->drupalCreateUser(array(
    'administer nodes',
    'bypass node access',
  ));
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet("node/{$node->nid}/revisions/{$vid}/view");
  $this
    ->assertNoResponse(403, 'Access to view revision granted');
  $this
    ->assertNoLinkByHref($path, 'No links on revision view.');
  $this
    ->assertNoLinkByHref('field-collection/field-test-collection/2/edit', 'No links on revision view.');
  $this
    ->assertNoLinkByHref('field-collection/field-test-collection/2/delete', 'No links on revision view.');
  $this
    ->drupalGet("node/{$node->nid}/revisions");
}