class NodeTitleFailoverTest in Freelinking 4.0.x
Same name and namespace in other branches
- 8.3 tests/src/Unit/Plugin/freelinking/NodeTitleFailoverTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTitleFailoverTest
Tests the failover options for nodetitle plugin.
@group freelinking
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTestBase uses \Prophecy\PhpUnit\ProphecyTrait
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTitleFailoverTest
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTestBase uses \Prophecy\PhpUnit\ProphecyTrait
Expanded class hierarchy of NodeTitleFailoverTest
File
- tests/
src/ Unit/ Plugin/ freelinking/ NodeTitleFailoverTest.php, line 15
Namespace
Drupal\Tests\freelinking\Unit\Plugin\freelinkingView source
class NodeTitleFailoverTest extends NodeTestBase {
/**
* Mocks container.
*
* @var \Drupal\Core\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* Tranlation interface mock.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationInterfaceMock;
/**
* {@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');
$this->translationInterfaceMock = $tProphet
->reveal();
// 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([]);
// 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');
$moduleHandlerProphet
->moduleExists('search')
->willReturn(TRUE);
$moduleHandlerProphet
->moduleExists('freelinking_prepopulate')
->willReturn(TRUE);
// Mock Path Validator.
$pathValidatorProphet = $this
->prophesize('\\Drupal\\Core\\Path\\PathValidatorInterface');
$container = new ContainerBuilder();
$container
->set('string_translation', $this->translationInterfaceMock);
$container
->set('entity_type.manager', $entityManagerProphet
->reveal());
$container
->set('path.validator', $pathValidatorProphet
->reveal());
$container
->set('module_handler', $moduleHandlerProphet
->reveal());
\Drupal::setContainer($container);
$this->container = $container;
}
/**
* Assert that failover option displays correctly.
*
* @param string $failoverOption
* The failover option to test.
* @param array $expected
* The expected render array.
*
* @dataProvider failoverProvider
*/
public function testFailover($failoverOption, array $expected) {
$plugin = NodeTitle::create($this->container, [
'settings' => [
'nodetypes' => [],
'failover' => $failoverOption,
],
], 'nodetitle', []);
// Mock the parsed target array.
$target = [
'dest' => 'Test Node',
'language' => $this
->getDefaultLanguage(),
'target' => 'Test Node|Test Node',
];
// Populate #url for the search test because container.
if ($failoverOption === 'error') {
$expected['#message'] = new TranslatableMarkup('Node title %target does not exist', [
'%target' => 'Test Node',
], [], $this->translationInterfaceMock);
}
$link = $plugin
->buildLink($target);
$this
->assertEquals($expected, $link);
}
/**
* Provide test parameters for ::testFailover.
*
* @return array
* An array of options and expected values.
*/
public function failoverProvider() {
$noneExpected = [
'#markup' => '[[nodetitle:Test Node|Test Node]]',
];
$showTextExpected = [
'error' => 'showtext',
];
$searchExpected = [
'error' => 'search',
];
$prepopulateExpected = [
'error' => 'prepopulate',
];
$errorExpected = [
'#theme' => 'freelink_error',
'#plugin' => 'nodetitle',
];
return [
[
'_none',
$noneExpected,
],
[
'showtext',
$showTextExpected,
],
[
'search',
$searchExpected,
],
[
'prepopulate',
$prepopulateExpected,
],
[
'error',
$errorExpected,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NodeTestBase:: |
public static | function | Get a language object to pass into the plugin. | |
NodeTitleFailoverTest:: |
protected | property | Mocks container. | |
NodeTitleFailoverTest:: |
protected | property | Tranlation interface mock. | |
NodeTitleFailoverTest:: |
public | function | Provide test parameters for ::testFailover. | |
NodeTitleFailoverTest:: |
protected | function |
Overrides UnitTestCase:: |
|
NodeTitleFailoverTest:: |
public | function | Assert that failover option displays correctly. | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
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 | 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:: |
public static | function |