You are here

public function EntityExampleTestCase::testEntityExampleBasic in Examples for Developers 7

Test Entity Example features.

  • CRUD
  • Table display
  • User access
  • Field management
  • Display management

File

entity_example/entity_example.test, line 72
Tests for entity_example module.

Class

EntityExampleTestCase
Functionality tests for entity example module.

Code

public function testEntityExampleBasic() {

  // Create 10 entities.
  for ($i = 1; $i <= 10; $i++) {
    $edit[$i]['item_description'] = $this
      ->randomName();
    $edit[$i]['entity_example_test_text[und][0][value]'] = $this
      ->randomName(32);
    $this
      ->drupalPost('examples/entity_example/basic/add', $edit[$i], 'Save');
    $this
      ->assertText('item_description=' . $edit[$i]['item_description']);
    $this
      ->drupalGet('examples/entity_example/basic/' . $i);
    $this
      ->assertText('item_description=' . $edit[$i]['item_description']);
    $this
      ->assertText($edit[$i]['entity_example_test_text[und][0][value]']);
  }

  // Delete entity 5.
  $this
    ->drupalPost('examples/entity_example/basic/5/edit', $edit[5], 'Delete');
  $this
    ->drupalGet('examples/entity_example/basic/5');
  $this
    ->assertResponse(404, 'Deleted entity 5 no longer exists');
  unset($edit[5]);

  // Update entity 2 and verify the update.
  $edit[2] = array(
    'item_description' => 'updated entity 2 ',
    'entity_example_test_text[und][0][value]' => 'updated entity 2 test text',
  );
  $this
    ->drupalPost('examples/entity_example/basic/2/edit', $edit[2], 'Save');
  $this
    ->assertText('item_description=' . $edit[2]['item_description']);
  $this
    ->assertText('updated entity 2 test text');

  // View the entity list page  and verify that the items which still exist
  // are there, and that the deleted #5 no longer is there.
  $this
    ->drupalGet('admin/structure/entity_example_basic/manage');
  foreach ($edit as $id => $item) {
    $this
      ->assertRaw('examples/entity_example/basic/' . $id . '">' . $item['item_description'] . '</a>');
  }
  $this
    ->assertNoRaw('examples/entity_example/basic/5">');

  // Add a field through the field UI and verify that it behaves correctly.
  $field_edit = array(
    'fields[_add_new_field][label]' => 'New junk field',
    'fields[_add_new_field][field_name]' => 'new_junk_field',
    'fields[_add_new_field][type]' => 'text',
    'fields[_add_new_field][widget_type]' => 'text_textfield',
  );
  $this
    ->drupalPost('admin/structure/entity_example_basic/manage/fields', $field_edit, t('Save'));
  $this
    ->drupalPost(NULL, array(), t('Save field settings'));
  $this
    ->drupalPost(NULL, array(), t('Save settings'));
  $this
    ->assertResponse(200);

  // Now verify that we can edit and view this entity with fields.
  $edit[10]['field_new_junk_field[und][0][value]'] = $this
    ->randomName();
  $this
    ->drupalPost('examples/entity_example/basic/10/edit', $edit[10], 'Save');
  $this
    ->assertResponse(200);
  $this
    ->assertText('item_description=' . $edit[10]['item_description']);
  $this
    ->assertText($edit[10]['field_new_junk_field[und][0][value]'], 'Custom field updated successfully');

  // Create and login user without view access.
  $account = $this
    ->drupalCreateUser(array(
    'access content',
  ));
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('admin/structure/entity_example_basic/manage');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('examples/entity_example/basic/2');
  $this
    ->assertResponse(403, 'User does not have permission to view entity');

  // Create and login user with view access but no edit access.
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'view any entity_example_basic entity',
  ));
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('admin/structure/entity_example_basic/manage');
  $this
    ->assertResponse(403, 'Denied access to admin manage page');
  $this
    ->drupalGet('examples/entity_example/basic/2');
  $this
    ->assertResponse(200, 'User has permission to view entity');
  $this
    ->drupalGet('examples/entity_example/basic/2/edit');
  $this
    ->assertResponse(403, 'User is denied edit privileges');

  // Create and login user with view and edit but no manage privs.
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'view any entity_example_basic entity',
    'edit any entity_example_basic entity',
  ));
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('admin/structure/entity_example_basic/manage');
  $this
    ->assertResponse(403, 'Denied access to admin manage page');
  $this
    ->drupalGet('examples/entity_example/basic/2');
  $this
    ->assertResponse(200, 'User has permission to view entity');
  $this
    ->drupalGet('examples/entity_example/basic/2/edit');
  $this
    ->assertResponse(200, 'User has edit privileges');
}