You are here

public function TemplateControllerTest::testJsCallback in Wysiwyg API template plugin 8.2

Same name and namespace in other branches
  1. 3.0.x tests/src/Kernel/Controller/TemplateControllerTest.php \Drupal\Tests\wysiwyg_template\Kernel\Controller\TemplateControllerTest::testJsCallback()

Tests the js callback.

File

tests/src/Kernel/Controller/TemplateControllerTest.php, line 67

Class

TemplateControllerTest
Tests the template controller object.

Namespace

Drupal\Tests\wysiwyg_template\Kernel\Controller

Code

public function testJsCallback() {

  // No node types passed, no templates.
  $request = $this->controller
    ->listJson();
  $expected = new stdClass();
  $expected->imagesPath = FALSE;
  self::assertEquals($expected, $this
    ->getJson($request
    ->getContent()));

  // Node type, no templates.
  $request = $this->controller
    ->listJson($this->nodeTypes[1]);
  self::assertEquals($expected, $this
    ->getJson($request
    ->getContent()));

  // Add a few non-node-specific templates.
  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();
  }

  // No node types passed, 5 templates should be available.
  $request = $this->controller
    ->listJson();
  $json = $this
    ->getJson($request
    ->getContent());
  foreach ($this->templates as $i => $template) {
    $this
      ->assertSame($template
      ->getBody(), $json->templates[$i]->html);
  }

  // Pass in a node type, and any templates not specifying node types should
  // be listed.
  $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);
  }

  // Add a node type to template 5, and change weight.
  $this->templates[4]
    ->set('node_types', [
    $this->nodeTypes[2]
      ->id(),
  ]);
  $this->templates[4]
    ->set('weight', -42);
  $this->templates[4]
    ->save();

  // Template 5 should not be in this list.
  $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);

  // Node type 2 should list all templates.
  $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);
}