You are here

public function WeightTest::testWeightSelectorBase in Weight 8.3

Test basic weight ordering.

File

tests/src/FunctionalJavascript/WeightTest.php, line 150

Class

WeightTest
Test basic functionality of weight.

Namespace

Drupal\Tests\weight\FunctionalJavascript

Code

public function testWeightSelectorBase() {
  $this
    ->drupalGet('test-weight');
  $page = $this
    ->getSession()
    ->getPage();
  $weight_select1 = $page
    ->findField("field_weight[0][weight]");
  $weight_select2 = $page
    ->findField("field_weight[1][weight]");
  $weight_select3 = $page
    ->findField("field_weight[2][weight]");
  $weight_select4 = $page
    ->findField("field_weight[3][weight]");

  // Check that rows weight selects are hidden.
  $this
    ->assertFalse($weight_select1
    ->isVisible());
  $this
    ->assertFalse($weight_select2
    ->isVisible());
  $this
    ->assertFalse($weight_select3
    ->isVisible());
  $this
    ->assertFalse($weight_select4
    ->isVisible());

  // Check that 'Article 2' row is heavier than 'Article 1' row.
  $this
    ->assertGreaterThan($weight_select1
    ->getValue(), $weight_select2
    ->getValue());

  // Check that 'Article 1' precedes the 'Article 2'.
  $this
    ->assertOrderInPage([
    'Article 1',
    'Article 2',
  ]);

  // Check that the 'unsaved changes' text is not present in the message area.
  $this
    ->assertSession()
    ->pageTextNotContains('You have unsaved changes.');

  // Drag and drop the 'Article 1' row over the 'Article 2' row.
  // @todo Test also the reverse, 'Article 2' over 'Article 1', when
  // https://www.drupal.org/node/2769825 is fixed.
  // @see https://www.drupal.org/node/2769825
  $dragged = $this
    ->xpath("//tr[@class='draggable'][1]//a[@class='tabledrag-handle']")[0];
  $target = $this
    ->xpath("//tr[@class='draggable'][2]//a[@class='tabledrag-handle']")[0];
  $dragged
    ->dragTo($target);

  // Give javascript some time to manipulate the DOM.
  $this
    ->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")');

  // Check that the 'unsaved changes' text appeared in the message area.
  $this
    ->assertSession()
    ->pageTextContains('You have unsaved changes.');

  // Check that 'Article 2' page precedes the 'Article 1'.
  $this
    ->assertOrderInPage([
    'Article 2',
    'Article 1',
  ]);
  $this
    ->submitForm([], 'Save');

  // @todo Fix the send message after saving the changes.
  // Check that page reordering was done in the backend for drag-n-drop.
  $page1 = Node::load($this->nodes[0]
    ->id());
  $page2 = Node::load($this->nodes[1]
    ->id());
  $this
    ->assertGreaterThan($page2->field_weight
    ->getString(), $page1->field_weight
    ->getString());

  // Check again that 'Article 2' is on top after form submit in the UI.
  $this
    ->assertOrderInPage([
    'Article 2',
    'Article 1',
  ]);

  // Toggle row weight selects as visible.
  $page
    ->findButton('Show row weights')
    ->click();

  // Check that rows weight selects are visible.
  $this
    ->assertTrue($weight_select1
    ->isVisible());
  $this
    ->assertTrue($weight_select2
    ->isVisible());

  // Check that 'Article 1' row became heavier than 'Article 2' row.
  $this
    ->assertGreaterThan($weight_select1
    ->getValue(), $weight_select2
    ->getValue());

  // Reverse again using the weight fields. Use the current values so the test
  // doesn't rely on knowing the values in the select boxes.
  $value1 = $weight_select1
    ->getValue();
  $value2 = $weight_select2
    ->getValue();
  $weight_select1
    ->setValue($value2);
  $weight_select2
    ->setValue($value1);

  // Toggle row weight selects back to hidden.
  $page
    ->findButton('Hide row weights')
    ->click();

  // Check that rows weight selects are hidden again.
  $this
    ->assertFalse($weight_select1
    ->isVisible());
  $this
    ->assertFalse($weight_select2
    ->isVisible());

  // @todo Fix the send message after saving the changes.
  $this
    ->submitForm([], 'Save');

  // Check that the 'Article 1' is first again.
  $this
    ->assertOrderInPage([
    'Article 1',
    'Article 2',
  ]);

  // Check that page reordering was done in the backend for manual weight
  // field usage.
  $page1 = Node::load($this->nodes[0]
    ->id());
  $page2 = Node::load($this->nodes[1]
    ->id());
  $this
    ->assertGreaterThan($page2->field_weight
    ->getString(), $page1->field_weight
    ->getString());

  // Check if the weight selector appear when is position in the view is the
  // first column.
  $this
    ->drupalGet('test-weight-first-position');
  $page = $this
    ->getSession()
    ->getPage();
  $weight_select1 = $page
    ->findField("field_weight[0][weight]");
  $weight_select2 = $page
    ->findField("field_weight[1][weight]");
  $weight_select3 = $page
    ->findField("field_weight[2][weight]");
  $weight_select4 = $page
    ->findField("field_weight[3][weight]");

  // Check that rows weight selects are hidden.
  $this
    ->assertFalse($weight_select1
    ->isVisible());
  $this
    ->assertFalse($weight_select2
    ->isVisible());
  $this
    ->assertFalse($weight_select3
    ->isVisible());
  $this
    ->assertFalse($weight_select4
    ->isVisible());

  // Drag and drop the 'Article 1' row over the 'Article 2' row.
  // @todo Test also the reverse, 'Article 2' over 'Article 1', when
  // https://www.drupal.org/node/2769825 is fixed.
  // @see https://www.drupal.org/node/2769825
  $dragged = $this
    ->xpath("//tr[@class='draggable'][1]//a[@class='tabledrag-handle']")[0];
  $target = $this
    ->xpath("//tr[@class='draggable'][2]//a[@class='tabledrag-handle']")[0];
  $dragged
    ->dragTo($target);

  // Give javascript some time to manipulate the DOM.
  $this
    ->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")');

  // Check that the 'unsaved changes' text appeared in the message area.
  $this
    ->assertSession()
    ->pageTextContains('You have unsaved changes.');

  // Check that 'Article 2' page precedes the 'Article 1'.
  $this
    ->assertOrderInPage([
    'Article 2',
    'Article 1',
  ]);
  $this
    ->submitForm([], 'Save');

  // @todo Fix the send message after saving the changes.
  // Test weight with a grouped views.
  $this
    ->drupalGet('test-weight-grouped');
  $page = $this
    ->getSession()
    ->getPage();
  $weight_select1 = $page
    ->findField("field_weight[0][weight]");
  $weight_select2 = $page
    ->findField("field_weight[1][weight]");
  $weight_select3 = $page
    ->findField("field_weight[2][weight]");
  $weight_select4 = $page
    ->findField("field_weight[3][weight]");

  // Check that rows weight selects are hidden.
  $this
    ->assertFalse($weight_select1
    ->isVisible());
  $this
    ->assertFalse($weight_select2
    ->isVisible());
  $this
    ->assertFalse($weight_select3
    ->isVisible());
  $this
    ->assertFalse($weight_select4
    ->isVisible());
}