Commit 8155ae67 authored by beagaliana's avatar beagaliana Committed by Pedro Cambra
Browse files

Issue #3266370 by beagaliana: Add Kernel test for WhatsApp block

parent 74fb60ed
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ block.settings.whatsapp_block:
  label: 'WhatsApp block'

whatsapp.settings:
  type: mapping
  type: config_object
  label: 'WhatsApp settings'
  mapping:
    widget_key:
+13 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies: {  }
id: whatsapp_test_key
label: 'John''s WhatsApp Key'
description: 'John Doe''s Key for WhatsApp widget'
key_type: authentication
key_type_settings: {  }
key_provider: config
key_provider_settings:
  key_value: muahahaha
key_input: text_field
key_input_settings: {  }
+1 −0
Original line number Diff line number Diff line
widget_key: whatsapp_test_key
+5 −0
Original line number Diff line number Diff line
name: WhatsApp Block Test
type: module
description: Module with key config for testing purposes
package: Testing
hidden: true
+93 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\whatsapp\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block\Traits\BlockCreationTrait;

/**
 * Tests the WhatsApp block.
 *
 * @group whatsapp
 */
class WhatsappBlockTest extends KernelTestBase {

  use BlockCreationTrait;

  /**
   * Modules to install.
   *
   * @var array
   */
  protected static $modules = ['block', 'whatsapp', 'whatsapp_test', 'key', 'system', 'user'];

  /**
   * The block being tested.
   *
   * @var \Drupal\whatsapp\Plugin\Block
   */
  protected $block;

  /**
   * The block storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected $storage;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The key value.
   *
   * @var string
   */
  protected $key = 'muahahaha';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['whatsapp_test', 'key', 'whatsapp']);
    $this->storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('block');

    $this->block = $this->storage->create([
      'id' => 'whatsappblock_test',
      'theme' => 'stark',
      'plugin' => 'whatsapp_block',
    ]);
    $this->block->save();

    $this->container->get('cache.render')->deleteAll();

    $this->renderer = $this->container->get('renderer');
  }

  /**
   * Tests the rendering of blocks.
   */
  public function testBasicRendering() {
    $entity = $this->storage->load('whatsappblock_test');
    $builder = $this->container->get('entity_type.manager')->getViewBuilder('block');
    $output = $builder->view($entity, 'block');

    $expected = [];
    $expected[] = '<div id="block-whatsappblock-test">';
    $expected[] = '  ';
    $expected[] = '    ';
    $expected[] = '      <script defer src="//widget.tochat.be/bundle.js?key=' . $this->key . '"></script>';
    $expected[] = '  </div>';
    $expected[] = '';
    $expected_output = implode("\n", $expected);
    $this->assertEquals($expected_output, $this->renderer->renderRoot($output));
  }

}