You are here

class PrintFormatTest in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/Plugin/PrintableFormat/PrintFormatTest.php \Drupal\Tests\printable\Unit\Plugin\PrintableFormat\PrintFormatTest

Tests the print format plugin.

@group Printable

Hierarchy

Expanded class hierarchy of PrintFormatTest

File

tests/src/Unit/Plugin/PrintableFormat/PrintFormatTest.php, line 13

Namespace

Drupal\Tests\printable\Unit\Plugin\PrintableFormat
View source
class PrintFormatTest extends UnitTestCase {

  /**
   * The plugin definition of this plugin.
   *
   * @var array
   */
  protected $pluginDefinition;

  /**
   * The ID of the plugin.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * The configuration to be passed into the format plugin constructor.
   *
   * @var array
   */
  protected $configuration;

  /**
   * {@inheritdoc}
   */
  public function __construct() {
    $this->pluginDefinition = [
      'description' => 'Print description.',
      'id' => 'print',
      'module' => 'printable',
      'title' => 'Print',
      'class' => 'Drupal\\printable\\Plugin\\PrintableFormat\\PrintFormat',
      'provider' => 'printable',
    ];
    $this->pluginId = 'print';
    $this->configuration = [];
  }

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return [
      'name' => 'Printable Format Base',
      'descriptions' => 'Tests the printable format base class.',
      'group' => 'Printable',
    ];
  }

  /**
   * Tests getting the plugin label from the plugin.
   *
   * @covers PrintFormat::GetLabel
   */
  public function testGetLabel() {
    $format = new PrintFormat($this->configuration, $this->pluginId, $this->pluginDefinition, $this
      ->getConfigFactoryStub(), $this
      ->getCssIncludeStub(), $this
      ->getLinkExtractorIncludeStub());
    $this
      ->assertEquals('Print', $format
      ->getLabel());
  }

  /**
   * Tests getting the plugin description from the plugin.
   *
   * @covers PrintFormat::GetDescription
   */
  public function testGetDescription() {
    $format = new PrintFormat($this->configuration, $this->pluginId, $this->pluginDefinition, $this
      ->getConfigFactoryStub(), $this
      ->getCssIncludeStub(), $this
      ->getLinkExtractorIncludeStub());
    $this
      ->assertEquals('Print description.', $format
      ->getDescription());
  }

  /**
   * Tests getting the default configuration for this plugin.
   *
   * @covers PrintFormat::DefaultConfiguration
   */
  public function testDefaultConfiguration() {
    $format = new PrintFormat($this->configuration, $this->pluginId, $this->pluginDefinition, $this
      ->getConfigFactoryStub(), $this
      ->getCssIncludeStub(), $this
      ->getLinkExtractorIncludeStub());
    $this
      ->assertEquals([
      'show_print_dialogue' => TRUE,
    ], $format
      ->defaultConfiguration());
  }

  /**
   * Tests getting the current configuration for this plugin.
   *
   * @covers PrintFormat::GetConfiguration
   */
  public function testGetConfiguration() {
    $format = new PrintFormat($this->configuration, $this->pluginId, $this->pluginDefinition, $this
      ->getConfigFactoryStub(), $this
      ->getCssIncludeStub(), $this
      ->getLinkExtractorIncludeStub(), $this
      ->getLinkExtractorIncludeStub());
    $this
      ->assertEquals([
      'show_print_dialogue' => TRUE,
    ], $format
      ->getConfiguration());
  }

  /**
   * Tests that additional configuration is internally stored and accessible.
   *
   * @covers PrintFormat::GetPassedInConfiguration
   */
  public function testGetPassedInConfiguration() {
    $format = new PrintFormat([
      'test_configuration_value' => TRUE,
    ], $this->pluginId, $this->pluginDefinition, $this
      ->getConfigFactoryStub(), $this
      ->getCssIncludeStub(), $this
      ->getLinkExtractorIncludeStub());
    $this
      ->assertEquals([
      'show_print_dialogue' => TRUE,
      'test_configuration_value' => TRUE,
    ], $format
      ->getConfiguration());
  }

  /**
   * Test that default configuration can be modified and changes accessed.
   *
   * @covers PrintFormat::SetConfiguration
   */
  public function testSetConfiguration() {
    $new_configuration = [
      'show_print_dialogue' => FALSE,
    ];
    $config_mock = $this
      ->getMockBuilder('\\Drupal\\Core\\Config\\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_mock
      ->expects($this
      ->once())
      ->method('set')
      ->with('print', $new_configuration)
      ->will($this
      ->returnSelf());
    $config_mock
      ->expects($this
      ->once())
      ->method('save');
    $config_factory_mock = $this
      ->getMockBuilder('\\Drupal\\Core\\Config\\ConfigFactory')
      ->disableOriginalConstructor()
      ->getMock();
    $config_factory_mock
      ->expects($this
      ->once())
      ->method('get')
      ->with('printable.format')
      ->will($this
      ->returnValue($config_mock));
    $format = new PrintFormat($this->configuration, $this->pluginId, $this->pluginDefinition, $config_factory_mock, $this
      ->getCssIncludeStub(), $this
      ->getLinkExtractorIncludeStub());
    $format
      ->setConfiguration($new_configuration);
    $this
      ->assertEquals($new_configuration, $format
      ->getConfiguration());
  }

  /**
   * Get the CSS include stub.
   */
  protected function getCssIncludeStub() {
    return $this
      ->getMockBuilder('Drupal\\printable\\PrintableCssIncludeInterface')
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Get the Link extractor stub.
   */
  protected function getLinkExtractorIncludeStub() {
    return $this
      ->getMockBuilder('Drupal\\printable\\LinkExtractor\\LinkExtractorInterface')
      ->disableOriginalConstructor()
      ->getMock();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
PrintFormatTest::$configuration protected property The configuration to be passed into the format plugin constructor.
PrintFormatTest::$pluginDefinition protected property The plugin definition of this plugin.
PrintFormatTest::$pluginId protected property The ID of the plugin.
PrintFormatTest::getCssIncludeStub protected function Get the CSS include stub.
PrintFormatTest::getInfo public static function
PrintFormatTest::getLinkExtractorIncludeStub protected function Get the Link extractor stub.
PrintFormatTest::testDefaultConfiguration public function Tests getting the default configuration for this plugin.
PrintFormatTest::testGetConfiguration public function Tests getting the current configuration for this plugin.
PrintFormatTest::testGetDescription public function Tests getting the plugin description from the plugin.
PrintFormatTest::testGetLabel public function Tests getting the plugin label from the plugin.
PrintFormatTest::testGetPassedInConfiguration public function Tests that additional configuration is internally stored and accessible.
PrintFormatTest::testSetConfiguration public function Test that default configuration can be modified and changes accessed.
PrintFormatTest::__construct public function
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.
UnitTestCase::setUp protected function 340