You are here

public function SmartReadMoreLinkUnitTest::testDelegationOfSettings in Smart read more link 8

Test that the formatter delegates its settings as expected.

File

tests/src/Unit/SmartReadMoreLinkUnitTest.php, line 26

Class

SmartReadMoreLinkUnitTest
Unit tests of smart_read_more_link module.

Namespace

Drupal\Tests\smart_read_more_link\Unit

Code

public function testDelegationOfSettings() {
  $field_definition = $this
    ->getMockBuilder(FieldDefinitionInterface::class)
    ->getMock();
  $base_formatter = $this
    ->getMockBuilder(FormatterBase::class)
    ->disableOriginalConstructor()
    ->getMock();
  $random_string1 = $this
    ->getRandomGenerator()
    ->string(20);
  $random_string2 = $this
    ->getRandomGenerator()
    ->string(20);
  $base_formatter
    ->method('settingsForm')
    ->willReturn([
    '#type' => 'markup',
    '#markup' => $random_string1,
  ]);
  $base_formatter
    ->method('settingsSummary')
    ->willReturn($random_string2);
  $default_formatter = $this
    ->getMockBuilder(FormatterBase::class)
    ->disableOriginalConstructor()
    ->getMock();
  $formatter_manager = $this
    ->getMockBuilder(PluginManagerInterface::class)
    ->getMock();
  $formatter_manager
    ->method('createInstance')
    ->willReturnCallback(static function ($plugin_id) use ($default_formatter, $base_formatter) {
    if ($plugin_id === 'text_summary_or_trimmed') {
      return $base_formatter;
    }
    else {
      return $default_formatter;
    }
  });
  $renderer = $this
    ->getMockBuilder(RendererInterface::class)
    ->getMock();
  $formatter = new SmartReadMoreLinkFormatter('smart_read_more_link', [], $field_definition, [], 'Field label', 'view_mode', [], $formatter_manager, $renderer);
  $form_state = $this
    ->getMockBuilder(FormState::class)
    ->getMock();
  $settings_form = $formatter
    ->settingsForm([], $form_state);
  $this
    ->assertEquals($settings_form['#markup'], $random_string1, 'delegates form to base formatter');
  $settings_summary = $formatter
    ->settingsSummary();
  $this
    ->assertEquals($settings_summary, $random_string2, 'delegates summary to base formatter');
}