View source
<?php
namespace Drupal\Tests\wysiwyg_template\Kernel\Controller;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\wysiwyg_template\Controller\TemplateController;
use Drupal\wysiwyg_template\Entity\Template;
use stdClass;
class TemplateControllerTest extends KernelTestBase {
public static $modules = [
'system',
'node',
'user',
'wysiwyg_template',
];
protected $controller;
protected $nodeTypes;
protected $templates;
public function setUp() {
parent::setUp();
$this->controller = TemplateController::create($this->container);
foreach (range(1, 3) as $i) {
$this->nodeTypes[$i] = NodeType::create([
'type' => strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$this->nodeTypes[$i]
->save();
}
}
public function testJsCallback() {
$request = $this->controller
->listJson();
$expected = new stdClass();
$expected->imagesPath = FALSE;
self::assertEquals($expected, $this
->getJson($request
->getContent()));
$request = $this->controller
->listJson($this->nodeTypes[1]);
self::assertEquals($expected, $this
->getJson($request
->getContent()));
foreach (range(0, 4) as $i) {
$this->templates[$i] = Template::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'body' => [
'value' => $this
->randomString(),
],
'node_types' => [],
'weight' => $i,
]);
$this->templates[$i]
->save();
}
$request = $this->controller
->listJson();
$json = $this
->getJson($request
->getContent());
foreach ($this->templates as $i => $template) {
$this
->assertSame($template
->getBody(), $json->templates[$i]->html);
}
$request = $this->controller
->listJson($this->nodeTypes[1]);
$json = $this
->getJson($request
->getContent());
foreach ($this->templates as $i => $template) {
$this
->assertSame($template
->getBody(), $json->templates[$i]->html);
}
$this->templates[4]
->set('node_types', [
$this->nodeTypes[2]
->id(),
]);
$this->templates[4]
->set('weight', -42);
$this->templates[4]
->save();
$request = $this->controller
->listJson($this->nodeTypes[1]);
$json = $this
->getJson($request
->getContent());
self::assertCount(4, $json->templates);
$this
->assertSame($this->templates[0]
->getBody(), $json->templates[0]->html);
$this
->assertSame($this->templates[1]
->getBody(), $json->templates[1]->html);
$this
->assertSame($this->templates[2]
->getBody(), $json->templates[2]->html);
$this
->assertSame($this->templates[3]
->getBody(), $json->templates[3]->html);
$request = $this->controller
->listJson($this->nodeTypes[2]);
$json = $this
->getJson($request
->getContent());
self::assertCount(5, $json->templates);
$this
->assertSame($this->templates[4]
->getBody(), $json->templates[0]->html);
$this
->assertSame($this->templates[0]
->getBody(), $json->templates[1]->html);
$this
->assertSame($this->templates[1]
->getBody(), $json->templates[2]->html);
$this
->assertSame($this->templates[2]
->getBody(), $json->templates[3]->html);
$this
->assertSame($this->templates[3]
->getBody(), $json->templates[4]->html);
}
protected function getJson($js) {
preg_match('/{.*}/', $js, $matches);
if (empty($matches[0])) {
$this
->fail('No json found in ' . $js);
}
return json_decode($matches[0], TRUE);
}
}