You are here

function DraggableviewsNativeHandlerTestCase::testSort in DraggableViews 7.2

File

test/draggableviews.test, line 127
Test cases file.

Class

DraggableviewsNativeHandlerTestCase
Testing Native Handler.

Code

function testSort() {
  $permissions = array(
    'access content',
  );
  $rid = $this
    ->drupalCreateRole($permissions);

  // Create five test users.
  $accounts = array();
  for ($i = 0; $i < 5; $i++) {
    $edit = array();
    $edit['name'] = $this
      ->randomName();

    // First three users should be prefixed 'test_'.
    if ($i < 3) {
      $edit['name'] = 'test_' . $edit['name'];
    }
    $edit['mail'] = $edit['name'] . '@example.com';
    $edit['roles'] = array(
      $rid => $rid,
    );
    $edit['pass'] = user_password();
    $edit['status'] = 1;
    $account = user_save(drupal_anonymous_user(), $edit);
    $account->pass_raw = $edit['pass'];
    $accounts[$account->uid] = $account;
  }
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'access draggableviews',
    'access user profiles',
    'access contextual links',
  ));
  $this
    ->drupalLogin($account);

  // Now lets sort and save a view.
  $edit = array(
    'draggableviews[0][weight]' => 0,
    'draggableviews[0][id]' => 1,
    'draggableviews[1][weight]' => 1,
    'draggableviews[1][id]' => 2,
    'draggableviews[2][weight]' => 2,
    'draggableviews[2][id]' => 3,
    'draggableviews[3][weight]' => 3,
    'draggableviews[3][id]' => 4,
    'draggableviews[4][weight]' => 4,
    'draggableviews[4][id]' => 5,
    'draggableviews[5][weight]' => 5,
    'draggableviews[5][id]' => 6,
    'draggableviews[6][weight]' => 6,
    'draggableviews[6][id]' => 7,
  );
  $this
    ->drupalPost('users-set', $edit, t('Save'));

  // Assert that first user is on first place, and second is on second.
  $first_row = $this
    ->xpath('//tr[@class="odd views-row-first draggable"]/td/a[@class="username"]');
  $second_row = $this
    ->xpath('//tr[@class="even draggable"]/td/a[@class="username"]');
  $this
    ->assertEqual((string) $first_row[0], 'placeholder-for...', t('First row user uid 1.'));
  $this
    ->assertEqual((string) $second_row[0], $accounts[2]->name, t('Second row user uid 2.'));

  // Now save a different sort (first and second rows changed places).
  $edit = array(
    'draggableviews[0][weight]' => 0,
    'draggableviews[0][id]' => 2,
    'draggableviews[1][weight]' => 1,
    'draggableviews[1][id]' => 1,
    'draggableviews[2][weight]' => 2,
    'draggableviews[2][id]' => 3,
    'draggableviews[3][weight]' => 3,
    'draggableviews[3][id]' => 4,
    'draggableviews[4][weight]' => 4,
    'draggableviews[4][id]' => 5,
    'draggableviews[5][weight]' => 5,
    'draggableviews[5][id]' => 6,
    'draggableviews[6][weight]' => 6,
    'draggableviews[6][id]' => 7,
  );
  $this
    ->drupalPost('users-set', $edit, t('Save'));

  // Assert that first user is on second place, and second user is on first.
  $first_row = $this
    ->xpath('//tr[@class="odd views-row-first draggable"]/td/a[@class="username"]');
  $second_row = $this
    ->xpath('//tr[@class="even draggable"]/td/a[@class="username"]');
  $this
    ->assertEqual((string) $first_row[0], $accounts[2]->name, t('First row user uid 2.'));
  $this
    ->assertEqual((string) $second_row[0], 'placeholder-for...', t('Second row user uid 1.'));

  // Apply exposed filter and set weights.
  $edit = array(
    'draggableviews[0][weight]' => 0,
    'draggableviews[0][id]' => 4,
    'draggableviews[1][weight]' => 1,
    'draggableviews[1][id]' => 3,
    'draggableviews[2][weight]' => 2,
    'draggableviews[2][id]' => 2,
  );
  $this
    ->drupalDraggableviewsPost('users-set', $edit, t('Save'), array(
    'query' => array(
      'mail' => 'test',
    ),
  ));

  // Now lets check display view page.
  $this
    ->drupalGet('users-display');
  $first_row = $this
    ->xpath('//tr[@class="odd views-row-first"]/td/a[@class="username"]');
  $second_row = $this
    ->xpath('//tr[@class="even"]/td/a[@class="username"]');
  $this
    ->assertEqual((string) $first_row[0], $accounts[2]->name, t('Display view. First row user uid 2.'));
  $this
    ->assertEqual((string) $second_row[0], 'placeholder-for...', t('Display view. Second row user uid 1.'));

  // Check display view with applied exposed filter.
  $this
    ->drupalGet('users-display', array(
    'query' => array(
      'mail' => 'test',
    ),
  ));
  $first_row = $this
    ->xpath('//tr[@class="odd views-row-first"]/td/a[@class="username"]');
  $second_row = $this
    ->xpath('//tr[@class="even"]/td/a[@class="username"]');
  $this
    ->assertEqual((string) $first_row[0], $accounts[4]->name, t('Display view. Exposed filter applied. First row user uid 4.'));
  $this
    ->assertEqual((string) $second_row[0], $accounts[3]->name, t('Display view. Exposed filter applied. Second row user uid 3.'));

  // Check contextual link existence.
  $contextual_links = $this
    ->xpath('//ul[@class="contextual-links views-contextual-links-page"]/li/a');
  $href = (string) $contextual_links[0]['href'];
  $this
    ->assertEqual($href, url('users-set', array(
    'query' => array(
      'destination' => 'users-display',
    ),
  )));
}