View source
<?php
namespace Drupal\Tests\book\FunctionalJavascript;
use Behat\Mink\Exception\ExpectationException;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\Node;
class BookJavascriptTest extends WebDriverTestBase {
protected static $modules = [
'book',
];
protected $defaultTheme = 'stark';
public function testBookOrdering() {
$book = Node::create([
'type' => 'book',
'title' => 'Book',
'book' => [
'bid' => 'new',
],
]);
$book
->save();
$page1 = Node::create([
'type' => 'book',
'title' => '1st page',
'book' => [
'bid' => $book
->id(),
'pid' => $book
->id(),
'weight' => 0,
],
]);
$page1
->save();
$page2 = Node::create([
'type' => 'book',
'title' => '2nd page',
'book' => [
'bid' => $book
->id(),
'pid' => $book
->id(),
'weight' => 1,
],
]);
$page2
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'administer book outlines',
]));
$this
->drupalGet('admin/structure/book/' . $book
->id());
$page = $this
->getSession()
->getPage();
$weight_select1 = $page
->findField("table[book-admin-{$page1->id()}][weight]");
$weight_select2 = $page
->findField("table[book-admin-{$page2->id()}][weight]");
$this
->assertFalse($weight_select1
->isVisible());
$this
->assertFalse($weight_select2
->isVisible());
$this
->assertGreaterThan($weight_select1
->getValue(), $weight_select2
->getValue());
$this
->assertOrderInPage([
'1st page',
'2nd page',
]);
$this
->assertSession()
->pageTextNotContains('You have unsaved changes.');
$dragged = $this
->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page1->id()}']//a[@class='tabledrag-handle']")[0];
$target = $this
->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page2->id()}']//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([
'2nd page',
'1st page',
]);
$this
->submitForm([], 'Save book pages');
$this
->assertSession()
->pageTextContains(new FormattableMarkup('Updated book @book.', [
'@book' => $book
->getTitle(),
]));
$page1 = Node::load($page1
->id());
$page2 = Node::load($page2
->id());
$this
->assertGreaterThan($page2->book['weight'], $page1->book['weight']);
$this
->assertOrderInPage([
'2nd page',
'1st page',
]);
$page
->findButton('Show row weights')
->click();
$this
->assertTrue($weight_select1
->isVisible());
$this
->assertTrue($weight_select2
->isVisible());
$this
->assertGreaterThan($weight_select2
->getValue(), $weight_select1
->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 book pages');
$this
->assertSession()
->pageTextContains(new FormattableMarkup('Updated book @book.', [
'@book' => $book
->getTitle(),
]));
$this
->assertOrderInPage([
'1st page',
'2nd page',
]);
$page1 = Node::load($page1
->id());
$page2 = Node::load($page2
->id());
$this
->assertGreaterThan($page2->book['weight'], $page1->book['weight']);
}
protected function assertOrderInPage(array $items) : void {
$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}.");
}
public function testBookAddOutline() {
$this
->drupalLogin($this
->drupalCreateUser([
'create book content',
'create new books',
'add content to books',
]));
$this
->drupalGet('node/add/book');
$assert_session = $this
->assertSession();
$session = $this
->getSession();
$page = $session
->getPage();
$page
->find('css', '#edit-book')
->click();
$book_select = $page
->findField("book[bid]");
$book_select
->setValue('new');
$assert_session
->waitForText('This will be the top-level page in this book.');
$assert_session
->pageTextContains('This will be the top-level page in this book.');
$assert_session
->pageTextNotContains('No book selected.');
$book_select
->setValue(0);
$assert_session
->waitForText('No book selected.');
$assert_session
->pageTextContains('No book selected.');
$assert_session
->pageTextNotContains('This will be the top-level page in this book.');
}
}