View source
<?php
namespace Drupal\Tests\weight\FunctionalJavascript;
use Behat\Mink\Exception\ExpectationException;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\views\Tests\ViewTestData;
use Drupal\node\Entity\Node;
class WeightTest extends WebDriverTestBase {
use FieldUiTestTrait;
protected static $modules = [
'node',
'views',
'weight',
'field_ui',
'weight_test_views',
];
protected $defaultTheme = 'stark';
protected static $fieldName = 'field_weight';
protected static $fieldType = 'weight';
protected $adminUser;
public static $testViews = [
'test_weight',
'test_weight_first_position',
'test_weight_grouped',
];
public $nodes = [];
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer content types',
'administer node fields',
'administer node display',
]);
$type = $this->container
->get('entity_type.manager')
->getStorage('node_type')
->create([
'type' => 'article',
'name' => 'Article',
]);
$type
->save();
$this->container
->get('router.builder')
->rebuild();
$fieldStorage = FieldStorageConfig::create([
'field_name' => static::$fieldName,
'entity_type' => 'node',
'type' => static::$fieldType,
]);
$fieldStorage
->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'article',
'required' => FALSE,
]);
$field
->save();
$nodes[] = [
'type' => 'article',
'title' => 'Article 1',
'field_weight' => -20,
];
$nodes[] = [
'type' => 'article',
'title' => 'Article 2',
'field_weight' => -19,
];
$nodes[] = [
'type' => 'article',
'title' => 'Article 3',
'field_weight' => -18,
];
$nodes[] = [
'type' => 'article',
'title' => 'Article 4',
'field_weight' => -17,
];
foreach ($nodes as $n) {
$node = $this
->drupalCreateNode($n);
$node
->save();
$this->nodes[] = $node;
}
ViewTestData::createTestViews(get_class($this), [
'weight_test_views',
]);
}
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]");
$this
->assertFalse($weight_select1
->isVisible());
$this
->assertFalse($weight_select2
->isVisible());
$this
->assertFalse($weight_select3
->isVisible());
$this
->assertFalse($weight_select4
->isVisible());
$this
->assertGreaterThan($weight_select1
->getValue(), $weight_select2
->getValue());
$this
->assertOrderInPage([
'Article 1',
'Article 2',
]);
$this
->assertSession()
->pageTextNotContains('You have unsaved changes.');
$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);
$this
->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")');
$this
->assertSession()
->pageTextContains('You have unsaved changes.');
$this
->assertOrderInPage([
'Article 2',
'Article 1',
]);
$this
->submitForm([], 'Save');
$page1 = Node::load($this->nodes[0]
->id());
$page2 = Node::load($this->nodes[1]
->id());
$this
->assertGreaterThan($page2->field_weight
->getString(), $page1->field_weight
->getString());
$this
->assertOrderInPage([
'Article 2',
'Article 1',
]);
$page
->findButton('Show row weights')
->click();
$this
->assertTrue($weight_select1
->isVisible());
$this
->assertTrue($weight_select2
->isVisible());
$this
->assertGreaterThan($weight_select1
->getValue(), $weight_select2
->getValue());
$value1 = $weight_select1
->getValue();
$value2 = $weight_select2
->getValue();
$weight_select1
->setValue($value2);
$weight_select2
->setValue($value1);
$page
->findButton('Hide row weights')
->click();
$this
->assertFalse($weight_select1
->isVisible());
$this
->assertFalse($weight_select2
->isVisible());
$this
->submitForm([], 'Save');
$this
->assertOrderInPage([
'Article 1',
'Article 2',
]);
$page1 = Node::load($this->nodes[0]
->id());
$page2 = Node::load($this->nodes[1]
->id());
$this
->assertGreaterThan($page2->field_weight
->getString(), $page1->field_weight
->getString());
$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]");
$this
->assertFalse($weight_select1
->isVisible());
$this
->assertFalse($weight_select2
->isVisible());
$this
->assertFalse($weight_select3
->isVisible());
$this
->assertFalse($weight_select4
->isVisible());
$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);
$this
->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")');
$this
->assertSession()
->pageTextContains('You have unsaved changes.');
$this
->assertOrderInPage([
'Article 2',
'Article 1',
]);
$this
->submitForm([], 'Save');
$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]");
$this
->assertFalse($weight_select1
->isVisible());
$this
->assertFalse($weight_select2
->isVisible());
$this
->assertFalse($weight_select3
->isVisible());
$this
->assertFalse($weight_select4
->isVisible());
}
protected function assertOrderInPage(array $items) {
$session = $this
->getSession();
$text = $session
->getPage()
->getHtml();
$strings = [];
foreach ($items as $item) {
if (($pos = strpos($text, $item)) === FALSE) {
throw new ExpectationException("Cannot find '{$item}' in the page", $session
->getDriver());
}
$strings[$pos] = $item;
}
ksort($strings);
$ordered = implode(', ', array_map(function ($item) {
return "'{$item}'";
}, $items));
$this
->assertSame($items, array_values($strings), "Found strings, ordered as: {$ordered}.");
}
}