2017-05-22 15:12:47 +01:00
< ? php
namespace Drupal\Tests\token\Kernel ;
use Drupal\node\Entity\Node ;
use Drupal\Core\Url ;
/**
* Test the book tokens .
*
* @ group token
*/
class BookTest extends KernelTestBase {
/**
* Modules to enable .
*
* @ var array
*/
public static $modules = [ 'user' , 'field' , 'filter' , 'text' , 'node' , 'book' ];
/**
* { @ inheritdoc }
*/
protected function setUp () {
parent :: setUp ();
$this -> installEntitySchema ( 'user' );
$this -> installEntitySchema ( 'node' );
2018-11-23 12:29:20 +00:00
$this -> installSchema ( 'book' , [ 'book' ]);
$this -> installSchema ( 'node' , [ 'node_access' ]);
$this -> installConfig ([ 'node' , 'book' , 'field' ]);
2017-05-22 15:12:47 +01:00
}
function testBookTokens () {
$book = Node :: create ([
'type' => 'book' ,
'title' => 'Book Main Page' ,
'book' => [ 'bid' => 'new' ],
]);
$book -> save ();
$page1 = Node :: create ([
'type' => 'book' ,
'title' => '1st Page' ,
'book' => [ 'bid' => $book -> id (), 'pid' => $book -> id ()],
]);
$page1 -> save ();
$page2 = Node :: create ([
'type' => 'book' ,
'title' => '2nd Page' ,
'book' => [ 'bid' => $book -> id (), 'pid' => $page1 -> id ()],
]);
$page2 -> save ();
$book_title = $book -> getTitle ();
$tokens = [
'nid' => $book -> id (),
'title' => $book_title ,
'book:title' => $book_title ,
'book:root' => $book_title ,
'book:root:nid' => $book -> id (),
'book:root:title' => $book_title ,
2018-11-23 12:29:20 +00:00
'book:root:url' => Url :: fromRoute ( 'entity.node.canonical' , [ 'node' => $book -> id ()], [ 'absolute' => TRUE ]) -> toString (),
2017-05-22 15:12:47 +01:00
'book:root:content-type' => 'Book page' ,
'book:parent' => null ,
'book:parents' => null ,
];
2018-11-23 12:29:20 +00:00
$this -> assertTokens ( 'node' , [ 'node' => $book ], $tokens );
2017-05-22 15:12:47 +01:00
$tokens = [
'nid' => $page1 -> id (),
'title' => $page1 -> getTitle (),
'book:title' => $book_title ,
'book:root' => $book_title ,
'book:root:nid' => $book -> id (),
'book:root:title' => $book_title ,
2018-11-23 12:29:20 +00:00
'book:root:url' => Url :: fromRoute ( 'entity.node.canonical' , [ 'node' => $book -> id ()], [ 'absolute' => TRUE ]) -> toString (),
2017-05-22 15:12:47 +01:00
'book:root:content-type' => 'Book page' ,
'book:parent:nid' => $book -> id (),
'book:parent:title' => $book_title ,
2018-11-23 12:29:20 +00:00
'book:parent:url' => Url :: fromRoute ( 'entity.node.canonical' , [ 'node' => $book -> id ()], [ 'absolute' => TRUE ]) -> toString (),
2017-05-22 15:12:47 +01:00
'book:parents:count' => 1 ,
'book:parents:join:/' => $book_title ,
];
2018-11-23 12:29:20 +00:00
$this -> assertTokens ( 'node' , [ 'node' => $page1 ], $tokens );
2017-05-22 15:12:47 +01:00
$tokens = [
'nid' => $page2 -> id (),
'title' => $page2 -> getTitle (),
'book:title' => $book_title ,
'book:root' => $book_title ,
'book:root:nid' => $book -> id (),
'book:root:title' => $book_title ,
2018-11-23 12:29:20 +00:00
'book:root:url' => Url :: fromRoute ( 'entity.node.canonical' , [ 'node' => $book -> id ()], [ 'absolute' => TRUE ]) -> toString (),
2017-05-22 15:12:47 +01:00
'book:root:content-type' => 'Book page' ,
'book:parent:nid' => $page1 -> id (),
'book:parent:title' => $page1 -> getTitle (),
2018-11-23 12:29:20 +00:00
'book:parent:url' => Url :: fromRoute ( 'entity.node.canonical' , [ 'node' => $page1 -> id ()], [ 'absolute' => TRUE ]) -> toString (),
2017-05-22 15:12:47 +01:00
'book:parents:count' => 2 ,
'book:parents:join:/' => $book_title . '/' . $page1 -> getTitle (),
];
2018-11-23 12:29:20 +00:00
$this -> assertTokens ( 'node' , [ 'node' => $page2 ], $tokens );
2017-05-22 15:12:47 +01:00
}
2018-11-23 12:29:20 +00:00
2017-05-22 15:12:47 +01:00
}