You are here

class BlockStyleTest in Block Style Plugins 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/Plugin/BlockStyleTest.php \Drupal\Tests\block_style_plugins\Unit\Plugin\BlockStyleTest

@coversDefaultClass \Drupal\block_style_plugins\Plugin\BlockStyle @group block_style_plugins

Hierarchy

Expanded class hierarchy of BlockStyleTest

File

tests/src/Unit/Plugin/BlockStyleTest.php, line 17

Namespace

Drupal\Tests\block_style_plugins\Unit\Plugin
View source
class BlockStyleTest extends UnitTestCase {

  /**
   * Mocked entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepository
   */
  protected $entityRepository;

  /**
   * Mocked entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Mocked form state.
   *
   * @var \Drupal\Core\Form\FormStateInterface
   */
  protected $formState;

  /**
   * Instance of the BlockStyle plugin.
   *
   * @var \Drupal\block_style_plugins\Plugin\BlockStyle
   */
  protected $plugin;

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

    // Stub the Iconset Finder Service.
    $this->entityRepository = $this
      ->prophesize(EntityRepositoryInterface::CLASS);

    // Stub the Entity Type Manager.
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::CLASS);

    // Form state double.
    $this->formState = $this
      ->prophesize(FormStateInterface::CLASS);
    $configuration = [];
    $plugin_id = 'block_style_plugins';
    $plugin_definition = [
      'provider' => 'block_style_plugins',
      'form' => [
        'test_field' => [
          '#type' => 'textfield',
          '#title' => 'this is a title',
          '#default_value' => 'default text',
        ],
        'second_field' => [
          '#type' => 'checkbox',
          '#title' => 'Checkbox title',
          '#default_value' => 1,
        ],
        'third_field' => [
          '#type' => 'textfield',
          '#title' => 'Third Box',
        ],
      ],
    ];
    $this->plugin = new BlockStyle($configuration, $plugin_id, $plugin_definition, $this->entityRepository
      ->reveal(), $this->entityTypeManager
      ->reveal());

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

  /**
   * Tests the defaultConfiguration method.
   *
   * @see ::defaultConfiguration()
   */
  public function testDefaultConfiguration() {
    $expected = [
      'test_field' => 'default text',
      'second_field' => 1,
    ];
    $default = $this->plugin
      ->defaultConfiguration();
    $this
      ->assertArrayEquals($expected, $default);
  }

  /**
   * Tests the buildConfigurationForm method.
   *
   * @see ::buildConfigurationForm()
   */
  public function testBuildConfigurationForm() {
    $expected = [
      'test_field' => [
        '#type' => 'textfield',
        '#title' => 'this is a title',
        '#default_value' => 'default text',
      ],
      'second_field' => [
        '#type' => 'checkbox',
        '#title' => 'Checkbox title',
        '#default_value' => 1,
      ],
      'third_field' => [
        '#type' => 'textfield',
        '#title' => 'Third Box',
        '#default_value' => 'user set value',
      ],
    ];

    // Use reflection to alter the protected $this->plugin->styles.
    $reflectionObject = new \ReflectionObject($this->plugin);
    $property = $reflectionObject
      ->getProperty('configuration');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($this->plugin, [
      'third_field' => 'user set value',
    ]);
    $form = [];
    $return = $this->plugin
      ->buildConfigurationForm($form, $this->formState
      ->reveal());
    $this
      ->assertArrayEquals($expected, $return);
  }

  /**
   * Tests the themeSuggestion method.
   *
   * @see ::themeSuggestion()
   */
  public function testThemeSuggestion() {
    $block = $this
      ->prophesize(ConfigEntityInterface::CLASS);
    $storage = $this
      ->prophesize(EntityStorageInterface::CLASS);
    $storage
      ->load(1)
      ->willReturn($block
      ->reveal());
    $this->entityTypeManager
      ->getStorage('block')
      ->willReturn($storage
      ->reveal());

    // Return the third party styles set in the plugin.
    $block
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
      ->willReturn([
      'class1',
      'class2',
    ]);

    // Use reflection to alter the protected $this->plugin->pluginDefinition.
    $reflectionObject = new \ReflectionObject($this->plugin);
    $property = $reflectionObject
      ->getProperty('pluginDefinition');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($this->plugin, [
      'template' => 'custom_template',
    ]);
    $suggestions = [];
    $variables = [
      'elements' => [
        '#id' => 1,
      ],
    ];
    $expected = [
      'custom_template',
    ];
    $return = $this->plugin
      ->themeSuggestion($suggestions, $variables);
    $this
      ->assertArrayEquals($expected, $return);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockStyleTest::$entityRepository protected property Mocked entity repository service.
BlockStyleTest::$entityTypeManager protected property Mocked entity type manager service.
BlockStyleTest::$formState protected property Mocked form state.
BlockStyleTest::$plugin protected property Instance of the BlockStyle plugin.
BlockStyleTest::setUp protected function Create the setup for constants and configFactory stub. Overrides UnitTestCase::setUp
BlockStyleTest::testBuildConfigurationForm public function Tests the buildConfigurationForm method.
BlockStyleTest::testDefaultConfiguration public function Tests the defaultConfiguration method.
BlockStyleTest::testThemeSuggestion public function Tests the themeSuggestion 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.