1 entity_example.test public EntityExampleTestCase::testEntityExampleBasic()

Test Entity Example features.

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

File

modules/examples/entity_example/tests/entity_example.test, line 60
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->backdropPost('examples/entity_example/basic/add', $edit[$i], 'Save');
    $this->assertText('item_description=' . $edit[$i]['item_description']);

    $this->backdropGet('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->backdropPost('examples/entity_example/basic/5/edit', $edit[5], 'Delete');
  $this->backdropGet('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->backdropPost('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->backdropGet('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->backdropPost('admin/structure/entity_example_basic/manage/fields', $field_edit, t('Save'));
  $this->backdropPost(NULL, array(), t('Save field settings'));
  $this->backdropPost(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->backdropPost('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->backdropCreateUser(array('access content'));
  $this->backdropLogin($account);
  $this->backdropGet('admin/structure/entity_example_basic/manage');
  $this->assertResponse(403);
  $this->backdropGet('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->backdropCreateUser(array('access content', 'view any entity_example_basic entity'));
  $this->backdropLogin($account);
  $this->backdropGet('admin/structure/entity_example_basic/manage');
  $this->assertResponse(403, 'Denied access to admin manage page');
  $this->backdropGet('examples/entity_example/basic/2');
  $this->assertResponse(200, 'User has permission to view entity');
  $this->backdropGet('examples/entity_example/basic/2/edit');
  $this->assertResponse(403, 'User is denied edit privileges');

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