class NodeTitleTest in Freelinking 8.3
Same name and namespace in other branches
- 4.0.x tests/src/Unit/Plugin/freelinking/NodeTitleTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTitleTest
Tests the nodetitle plugin behavior.
@group freelinking
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTestBase
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTitleTest
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTestBase
Expanded class hierarchy of NodeTitleTest
File
- tests/
src/ Unit/ Plugin/ freelinking/ NodeTitleTest.php, line 15
Namespace
Drupal\Tests\freelinking\Unit\Plugin\freelinkingView source
class NodeTitleTest extends NodeTestBase {
/**
* Freelinking plugin.
*
* @var \Drupal\freelinking\Plugin\FreelinkingPluginInterface
*/
protected $plugin;
/**
* {@inheritdoc}
*/
protected function setUp() {
// Mock the translation service.
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Click to view a local node');
// Mock Entity Type Manager.
$entityManagerProphet = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
// Mock Entity Query via Mock Builder to support chaining.
$entityQuery = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')
->disableOriginalConstructor()
->getMock();
$entityQuery
->expects($this
->any())
->method('condition')
->willReturnSelf();
$entityQuery
->expects($this
->any())
->method('accessCheck')
->willReturnSelf();
$entityQuery
->expects($this
->any())
->method('execute')
->willReturn([
1 => 1,
2 => 2,
]);
// Mock Node Storage.
$nodeStorageProphet = $this
->prophesize('\\Drupal\\node\\NodeStorageInterface');
$nodeStorageProphet
->getQuery('AND')
->willReturn($entityQuery);
// Mock Entity Type Manager getStorage.
$entityManagerProphet
->getStorage('node')
->wilLReturn($nodeStorageProphet
->reveal());
// Mock Module Handler.
$moduleHandlerProphet = $this
->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$container = new ContainerBuilder();
$container
->set('string_translation', $tProphet
->reveal());
$container
->set('entity_type.manager', $entityManagerProphet
->reveal());
$container
->set('module_handler', $moduleHandlerProphet
->reveal());
\Drupal::setContainer($container);
$this->plugin = NodeTitle::create($container, [
'settings' => [
'nodetypes' => [],
'failover' => '',
],
], 'nodetitle', []);
}
/**
* Assert that getTip returns TranslatableMarkup.
*/
public function testGetTip() {
$this
->assertEquals('Click to view a local node', $this->plugin
->getTip()
->render());
}
/**
* Assert that getIndicator is a pattern.
*
* @param string $test
* The string to test the pattern against.
* @param int $expected
* The expected return from preg_match().
*
* @dataProvider indicatorProvider
*/
public function testGetIndicator($test, $expected) {
$this
->assertEquals($expected, preg_match($this->plugin
->getIndicator(), $test));
}
/**
* Assert the default configuration.
*/
public function testDefaultConfiguration() {
$this
->assertEquals([
'settings' => [
'nodetypes' => [],
'failover' => '',
],
], $this->plugin
->defaultConfiguration());
}
/**
* Assert that build link will return a render array.
*/
public function testBuildLink() {
$language = self::getDefaultLanguage();
$expected = [
'#type' => 'link',
'#title' => 'Test Node',
'#url' => Url::fromRoute('entity.node.canonical', [
'node' => 1,
], [
'language' => $language,
]),
'#attributes' => [
'title' => $this->plugin
->getTip(),
],
];
$target = [
'dest' => 'Test Node',
'language' => $language,
];
$this
->assertEquals($expected, $this->plugin
->buildLink($target));
}
/**
* Provide strings to test indicator pattern with expected result.
*
* @return array
* An array of test method arguments.
*/
public function indicatorProvider() {
return [
[
'ntnomatch',
0,
],
[
'nt',
1,
],
[
'nodetitle',
1,
],
[
'title',
1,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NodeTestBase:: |
public static | function | Get a language object to pass into the plugin. | |
NodeTitleTest:: |
protected | property | Freelinking plugin. | |
NodeTitleTest:: |
public | function | Provide strings to test indicator pattern with expected result. | |
NodeTitleTest:: |
protected | function |
Overrides UnitTestCase:: |
|
NodeTitleTest:: |
public | function | Assert that build link will return a render array. | |
NodeTitleTest:: |
public | function | Assert the default configuration. | |
NodeTitleTest:: |
public | function | Assert that getIndicator is a pattern. | |
NodeTitleTest:: |
public | function | Assert that getTip returns TranslatableMarkup. | |
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. | |
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. |