You are here

class PdbBlockTest in Decoupled Blocks 8

@coversDefaultClass \Drupal\pdb\Plugin\Block\PdbBlock @group pdb

Hierarchy

Expanded class hierarchy of PdbBlockTest

File

tests/src/Unit/Plugin/Block/PdbBlockTest.php, line 17

Namespace

Drupal\Tests\pdb\Unit\Plugin\Block
View source
class PdbBlockTest extends UnitTestCase {

  /**
   * Instance of the Plugin.
   *
   * @var \Drupal\pdb\Plugin\Block\PdbBlock
   */
  protected $plugin;

  /**
   * Create the setup for constants and plugin instance.
   */
  protected function setUp() {
    parent::setUp();

    // Mock the UUID service.
    $uuid = $this
      ->prophesize(UuidInterface::CLASS);
    $uuid
      ->generate()
      ->willReturn('uuid');
    $context_definition = $this
      ->prophesize(EntityContextDefinition::CLASS);

    // Create a container needed by PdbBlock.
    $container = new ContainerBuilder();
    $container
      ->set('uuid', $uuid
      ->reveal());
    \Drupal::setContainer($container);
    $configuration = [
      'pdb_configuration' => [
        'testField' => 'test',
        'second_field' => 1,
        'formatted_field' => [
          'value' => '<p>Formatted text</p>',
        ],
      ],
    ];
    $plugin_id = 'pdb';
    $plugin_definition = [
      'provider' => 'pdb',
      'admin_label' => 'test',
      'info' => [
        'machine_name' => 'example-1',
        'add_js' => [
          'footer' => [
            'example-1.js' => [],
          ],
        ],
        'settings' => [
          'pdb' => [
            'settings test',
          ],
        ],
        'configuration' => [
          'testField' => [
            'type' => 'textfield',
            'default_value' => 'test',
          ],
          'formatted_field' => [
            'type' => 'text_format',
            'allowed_formats' => [
              'basic_html',
            ],
          ],
        ],
      ],
      'context_definitions' => [
        'entity' => $context_definition
          ->reveal(),
      ],
    ];

    // Create a new instance from the Abstract Class.
    $anonymous_class_from_abstract = new class($configuration, $plugin_id, $plugin_definition) extends PdbBlock {
      public function returnThis() {
        return $this;
      }
      public function attachFramework(array $component) {
        return [
          'drupalSettings' => [
            'pdb' => [
              'webcomponents' => [],
            ],
          ],
        ];
      }
      public function attachPageHeader(array $component) {
        return [
          'page_attachment',
        ];
      }
      protected function getContextsValues(array $contexts) {
        return [
          'context_key' => 'context_value',
        ];
      }
      protected function getJsContexts(array $contexts) {
        return [
          'context_key' => 'context_value',
        ];
      }
      protected function addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts) {
        return 'context';
      }

    };
    $this->plugin = $anonymous_class_from_abstract
      ->returnThis();

    // Create a translation stub for the t() method
    $translator = $this
      ->getStringTranslationStub();
    $this->plugin
      ->setStringTranslation($translator);
  }

  /**
   * Tests the build() method.
   */
  public function testBuild() {
    $expected = [
      '#attached' => [
        'drupalSettings' => [
          'pdb' => [
            'settings test',
            'webcomponents' => [],
            'configuration' => [
              'uuid' => [
                'testField' => 'test',
                'second_field' => 1,
                'formatted_field' => [
                  'value' => '<p>Formatted text</p>',
                ],
              ],
            ],
            'contexts' => [
              'context_key' => 'context_value',
            ],
          ],
        ],
        'pdb/example-1/footer',
        'page_attachment',
      ],
    ];
    $return = $this->plugin
      ->build();
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Tests the attachLibraries() method.
   *
   * @dataProvider attachLibrariesProvider
   */
  public function testAttachLibraries($value, $expected) {
    $component = [
      'machine_name' => 'example-1',
    ];
    $component = array_merge($component, $value);
    $return = $this->plugin
      ->attachLibraries($component);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Provider for testAttachLibraries().
   */
  public function attachLibrariesProvider() {
    return [
      [
        [
          'add_js' => [
            'header' => [
              'example-1.js' => [],
            ],
          ],
        ],
        [
          'pdb/example-1/header',
        ],
      ],
      [
        [
          'add_css' => [
            'header' => [
              'example-1.css' => [],
            ],
          ],
        ],
        [
          'pdb/example-1/header',
        ],
      ],
      [
        [
          'add_css' => [
            'header' => [
              'example-1.css' => [],
            ],
          ],
          'add_js' => [
            'header' => [
              'example-1.js' => [],
            ],
          ],
        ],
        [
          'pdb/example-1/header',
        ],
      ],
      [
        [
          'add_js' => [
            'footer' => [
              'example-1.js' => [],
            ],
          ],
        ],
        [
          'pdb/example-1/footer',
        ],
      ],
      [
        [
          'add_css' => [
            'footer' => [
              'example-1.css' => [],
            ],
          ],
        ],
        [
          'pdb/example-1/footer',
        ],
      ],
      [
        [
          'add_css' => [
            'footer' => [
              'example-1.css' => [],
            ],
          ],
          'add_js' => [
            'footer' => [
              'example-1.js' => [],
            ],
          ],
        ],
        [
          'pdb/example-1/footer',
        ],
      ],
      [
        [
          'add_css' => [
            'header' => [
              'example-1.css' => [],
            ],
            'footer' => [
              'example-1.css' => [],
            ],
          ],
          'add_js' => [
            'header' => [
              'example-1.js' => [],
            ],
            'footer' => [
              'example-1.js' => [],
            ],
          ],
        ],
        [
          'pdb/example-1/header',
          'pdb/example-1/footer',
        ],
      ],
      [
        [
          'add_css' => [
            'header' => [
              'css' => [
                'example-1.css' => [],
              ],
              'dependencies' => [
                'pdb/example-2/header',
              ],
            ],
            'footer' => [
              'css' => [
                'example-1.css' => [],
              ],
              'dependencies' => [
                'pdb/example-2/footer',
              ],
            ],
          ],
          'add_js' => [
            'header' => [
              'js' => [
                'example-1.js' => [],
              ],
              'dependencies' => [
                'pdb/example-2/header',
              ],
            ],
            'footer' => [
              'js' => [
                'example-1.js' => [],
              ],
              'dependencies' => [
                'pdb/example-2/footer',
              ],
            ],
          ],
        ],
        [
          'pdb/example-1/header',
          'pdb/example-1/footer',
        ],
      ],
    ];
  }

  /**
   * Tests the attachSettings() method.
   */
  public function testAttachSettings() {
    $component = [
      'settings' => [
        'pdb' => [
          'foobar',
        ],
      ],
    ];
    $expected = [
      'drupalSettings' => [
        'pdb' => [
          'foobar',
        ],
      ],
    ];
    $return = $this->plugin
      ->attachSettings($component);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Tests the buildConfigurationForm() method.
   */
  public function testBuildConfigurationForm() {
    $form_state = $this
      ->createMock(FormStateInterface::CLASS);
    $return = $this->plugin
      ->buildConfigurationForm([], $form_state);
    $this
      ->assertEquals('test', $return['pdb_configuration']['testField']['#default_value']);
    $this
      ->assertEquals('<p>Formatted text</p>', $return['pdb_configuration']['formatted_field']['#default_value']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PdbBlockTest::$plugin protected property Instance of the Plugin.
PdbBlockTest::attachLibrariesProvider public function Provider for testAttachLibraries().
PdbBlockTest::setUp protected function Create the setup for constants and plugin instance. Overrides UnitTestCase::setUp
PdbBlockTest::testAttachLibraries public function Tests the attachLibraries() method.
PdbBlockTest::testAttachSettings public function Tests the attachSettings() method.
PdbBlockTest::testBuild public function Tests the build() method.
PdbBlockTest::testBuildConfigurationForm public function Tests the buildConfigurationForm() method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.