You are here

public function FeedsAccountSwitcherTest::testAuthorizedImport in Feeds 7.2

Tests if an extra account switch happens on authorized imports.

File

tests/FeedsAccountSwitcherTest.test, line 148
Contains FeedsAccountSwitcherTest.

Class

FeedsAccountSwitcherTest
Test case for account switching.

Code

public function testAuthorizedImport() {

  // Enable feeds_test_field module.
  module_enable(array(
    'feeds_test_field',
  ));

  // Create content type.
  $typename = $this
    ->createContentType(array(), array(
    'alpha' => array(
      'type' => 'feeds_test_field',
      'widget' => 'feeds_test_field_textfield',
    ),
  ));

  // Create a role with permission to create content and permission to edit
  // fields of type 'feeds_test_field'.
  $rid1 = $this
    ->drupalCreateRole(array(
    'access content',
    'create ' . $typename . ' content',
    'feeds_test_field.edit',
  ));

  // Create also a role that only may create content, but may NOT edit fields
  // of type 'feeds_test_field'.
  $rid2 = $this
    ->drupalCreateRole(array(
    'access content',
    'create ' . $typename . ' content',
  ));

  // Create one account that may create content and an other
  // who may not.
  $morticia = user_save(drupal_anonymous_user(), array(
    'name' => 'Morticia',
    'mail' => 'morticia@example.com',
    'pass' => 'mort',
    'status' => 1,
    'roles' => array(
      $rid1 => $rid1,
    ),
  ));

  // Fester may not edit feeds_test_field fields.
  $fester = user_save(drupal_anonymous_user(), array(
    'name' => 'Fester',
    'mail' => 'fester@example.com',
    'pass' => 'fester',
    'status' => 1,
    'roles' => array(
      $rid2 => $rid2,
    ),
  ));

  // Assert that the admin user is logged in. After import, we check again
  // which user is logged in. It is important to ensure that it's the same
  // user who's logged in after import, so we can ensure that account switches
  // get reverted properly.
  $this
    ->drupalGet('user');
  $this
    ->clickLink('Edit');
  $this
    ->assertUrl('user/' . $this->admin_user->uid . '/edit');

  // Use the CSV parser.
  $this
    ->setPlugin('syndication', 'FeedsCSVParser');

  // Turn on authorize option and set bundle.
  $this
    ->setSettings('syndication', 'FeedsNodeProcessor', array(
    'authorize' => TRUE,
    'bundle' => $typename,
  ));

  // The column 'author' from the CSV contains the username.
  $this
    ->addMappings('syndication', array(
    1 => array(
      'source' => 'author',
      'target' => 'user_name',
    ),
    2 => array(
      'source' => 'alpha',
      'target' => 'field_alpha',
    ),
  ));

  // Create a feed node and change the author of the node. During the import,
  // Feeds will initially switch to the author's account. This author is
  // allowed to create content, but not edit fields of type
  // 'feeds_test_field'. Since the import requires that permission, we can
  // ensure another account switch happened if the content gets imported
  // successfully.
  $nid = $this
    ->createFeedNode('syndication', $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content_author.csv', 'Node 1');
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'create ' . $typename . ' content',
  ));
  $this
    ->changeNodeAuthor($nid, $account);

  // And perform import.
  $this
    ->drupalPost('node/' . $nid . '/import', NULL, 'Import');

  // Assert that only one node was imported. Only the author of the first item
  // is expected to be allowed to edit field_alpha, but the author of the
  // second item isn't.
  $this
    ->assertText('Created 1 node');
  $this
    ->assertText('Failed importing 1 node');
  $this
    ->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
  $this
    ->assertText('You are not authorized to edit this field');

  // Assert that the admin user is still logged in. This ensures that after
  // the import the account switch has been reverted.
  $this
    ->drupalGet('user');
  $this
    ->clickLink('Edit');
  $this
    ->assertUrl('user/' . $this->admin_user->uid . '/edit');
}