class SmartReadMoreLinkUnitTest in Smart read more link 8
Unit tests of smart_read_more_link module.
@group smart_read_more_link
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\smart_read_more_link\Unit\SmartReadMoreLinkUnitTest
Expanded class hierarchy of SmartReadMoreLinkUnitTest
File
- tests/
src/ Unit/ SmartReadMoreLinkUnitTest.php, line 21
Namespace
Drupal\Tests\smart_read_more_link\UnitView source
class SmartReadMoreLinkUnitTest extends UnitTestCase {
/**
* Test that the formatter delegates its settings as expected.
*/
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');
}
/**
* Test no link is generated when teaser and full views match.
*/
public function testWhenTeaserMatchesFull() {
$field_definition = $this
->getMockBuilder(FieldDefinitionInterface::class)
->getMock();
$base_formatter = $this
->getMockBuilder(FormatterBase::class)
->disableOriginalConstructor()
->getMock();
$random_string1 = $this
->getRandomGenerator()
->string(20);
$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();
// Rendered view is constant.
$renderer
->method('render')
->willReturn($random_string1);
$formatter = new SmartReadMoreLinkFormatter('smart_read_more_link', [], $field_definition, [], 'Field label', 'view_mode', [], $formatter_manager, $renderer);
$items = $this
->getMockBuilder(FieldItemListInterface::class)
->getMock();
$result = $formatter
->viewElements($items, 'und');
// @todo be more thorough about this
$this
->assertEmpty($result[0]['links'], 'no link in output');
}
/**
* Test no link is generated when teaser and full views match.
*/
public function testWhenTeaserDoesNotMatchFull() {
$field_definition = $this
->getMockBuilder(FieldDefinitionInterface::class)
->getMock();
$base_formatter = $this
->getMockBuilder(FormatterBase::class)
->disableOriginalConstructor()
->getMock();
$base_formatter
->method('viewElements')
->willReturn([
[
'#markup' => 'anything',
],
]);
$random_string1 = $this
->getRandomGenerator()
->string(200);
$random_string2 = $this
->getRandomGenerator()
->string(100);
$default_formatter = $this
->getMockBuilder(FormatterBase::class)
->disableOriginalConstructor()
->getMock();
$default_formatter
->method('viewElements')
->willReturn([
[
'#markup' => 'otherthing',
],
]);
$formatter_manager = $this
->getMockBuilder(PluginManagerInterface::class)
->getMock();
$formatter_manager
->method('createInstance')
->willReturnCallback(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();
// Rendered view is constant.
$renderer
->method('render')
->willReturnOnConsecutiveCalls($random_string1, $random_string2);
$formatter = new SmartReadMoreLinkFormatter('smart_read_more_link', [], $field_definition, [], 'Field label', 'view_mode', [], $formatter_manager, $renderer);
$entity = $this
->getMockBuilder(EntityBase::class)
->disableOriginalConstructor()
->getMock();
$entity
->method('label')
->willReturn('Entity label');
$url = $this
->getMockBuilder(Url::class)
->disableOriginalConstructor()
->getMock();
$entity
->method('urlInfo')
->willReturn($url);
$entity
->method('language')
->willReturn('und');
$items = $this
->getMockBuilder(FieldItemListInterface::class)
->getMock();
$items
->method('getEntity')
->willReturn($entity);
$formatter
->setStringTranslation($this
->getStringTranslationStub());
$result = $formatter
->viewElements($items, 'und');
// @todo be more thorough about this
$this
->assertNotEmpty($result[0]['links'], 'link in output');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SmartReadMoreLinkUnitTest:: |
public | function | Test that the formatter delegates its settings as expected. | |
SmartReadMoreLinkUnitTest:: |
public | function | Test no link is generated when teaser and full views match. | |
SmartReadMoreLinkUnitTest:: |
public | function | Test no link is generated when teaser and full views match. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |