<?php

/**
 * @file
 * Tests for entity_example module.
 *
 * Verify example module functionality.
 */

/**
 * Functionality tests for enitity example module.
 *
 * @ingroup entity_example
 */
class EntityExampleTestCase extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Entity example',
      'description' => 'Basic entity example tests',
      'group' => 'Examples',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    // Enable the module.
    parent::setUp('entity_example');

    // Create and login user with access.
    $permissions = array(
      'access content',
      'view any entity_example_basic entity',
      'edit any entity_example_basic entity',
      'create entity_example_basic entities',
      'administer entity_example_basic entities',
      'administer site configuration',
    );
    $account = $this->drupalCreateUser($permissions);
    $this->drupalLogin($account);

    // Attach a field.
    $field = array(
      'field_name' => 'entity_example_test_text' ,
      'type' => 'text',
    );
    field_create_field($field);
    $instance = array(
      'label' => 'Subject',
      'field_name' => 'entity_example_test_text',
      'entity_type' => 'entity_example_basic',
      'bundle' => 'first_example_bundle',
    );
    field_create_instance($instance);
  }

  /**
   * Test Entity Example features.
   *
   * - CRUD
   * - Table display
   * - User access
   * - Field management
   * - Display management
   */
  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');
  }
}
