You are here

public function DraggableviewsFieldAPIHandlerTestCase::testSort in DraggableViews 7.2

File

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

Class

DraggableviewsFieldAPIHandlerTestCase
Testing Field API Handler.

Code

public function testSort() {
  $this
    ->createField();
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'access draggableviews',
    'access user profiles',
    'access contextual links',
  ));
  $this
    ->drupalLogin($account);

  // Create five nodes.
  $nodes = array();
  for ($i = 0; $i < 5; $i++) {
    $node = $this
      ->drupalCreateNode(array(
      'type' => 'article',
    ));
    $nodes[$node->nid] = $node;
  }

  // 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,
  );
  $this
    ->drupalPost('nodes-set', $edit, t('Save'));

  // Assert that first node is on first place, and second is on second.
  $first_row = $this
    ->xpath('//tr[@class="odd views-row-first draggable"]/td/a');
  $second_row = $this
    ->xpath('//tr[@class="even draggable"]/td/a');
  $this
    ->assertEqual((string) $first_row[0], $nodes[1]->title, t('First row node nid 1.'));
  $this
    ->assertEqual((string) $second_row[0], $nodes[2]->title, t('Second row node nid 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,
  );
  $this
    ->drupalPost('nodes-set', $edit, t('Save'));

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

  // Check display view order.
  $this
    ->drupalGet('nodes-display');
  $first_row = $this
    ->xpath('//tr[@class="odd views-row-first"]/td/a');
  $second_row = $this
    ->xpath('//tr[@class="even"]/td/a');
  $this
    ->assertEqual((string) $first_row[0], $nodes[2]->title, t('First row node nid 2.'));
  $this
    ->assertEqual((string) $second_row[0], $nodes[1]->title, t('Second row node nid 1.'));

  // Check values of nodes.
  $node1 = node_load(1);
  $node2 = node_load(2);
  $this
    ->assertTrue($node1->field_weight[LANGUAGE_NONE][0]['value'] > $node2->field_weight[LANGUAGE_NONE][0]['value'], t('Weight of node 1 is more than weight of node 2.'));
}