Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- master

env:
CI_COVERAGE_THRESHOLD: '10'
CI_COVERAGE_THRESHOLD: '35'

jobs:
tests:
Expand Down
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
<include>
<directory>./src/Drupal/</directory>
</include>
<exclude>
<directory>./src/Drupal/Driver/Cores/Drupal6</directory>
<directory>./src/Drupal/Driver/Cores/Drupal7</directory>
<directory>./src/Drupal/Driver/Fields/Drupal6</directory>
<directory>./src/Drupal/Driver/Fields/Drupal7</directory>
<file>./src/Drupal/Driver/Cores/Drupal6.php</file>
<file>./src/Drupal/Driver/Cores/Drupal7.php</file>
</exclude>
</source>
<coverage pathCoverage="false"
ignoreDeprecatedCodeUnits="true">
Expand Down
105 changes: 105 additions & 0 deletions tests/Drupal/Tests/Driver/BlackboxDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Drupal\Tests\Driver;

use Drupal\Driver\BlackboxDriver;
use Drupal\Driver\Exception\UnsupportedDriverActionException;
use PHPUnit\Framework\TestCase;

/**
* Tests the BlackboxDriver (and by extension BaseDriver).
*/
class BlackboxDriverTest extends TestCase {

/**
* Tests that BlackboxDriver considers itself bootstrapped.
*/
public function testIsBootstrappedReturnsTrue() {
$driver = new BlackboxDriver();
$this->assertTrue($driver->isBootstrapped());
}

/**
* Tests that bootstrap() is a no-op.
*/
public function testBootstrapIsNoop() {
$driver = new BlackboxDriver();
$this->assertNull($driver->bootstrap());
}

/**
* Tests that isField() always returns FALSE in the blackbox driver.
*/
public function testIsFieldReturnsFalse() {
$driver = new BlackboxDriver();
$this->assertFalse($driver->isField('node', 'field_body'));
}

/**
* Tests that isBaseField() always returns FALSE in the blackbox driver.
*/
public function testIsBaseFieldReturnsFalse() {
$driver = new BlackboxDriver();
$this->assertFalse($driver->isBaseField('node', 'title'));
}

/**
* Tests that unsupported driver actions throw the expected exception.
*
* @param string $method
* The BaseDriver method name to invoke.
* @param array $args
* Positional arguments to pass to the method.
* @param string $message_fragment
* A substring that must appear in the exception message.
*
* @dataProvider dataProviderUnsupportedActionsThrow
*/
public function testUnsupportedActionsThrow($method, array $args, $message_fragment) {
$driver = new BlackboxDriver();

$this->expectException(UnsupportedDriverActionException::class);
$this->expectExceptionMessageMatches('/' . preg_quote($message_fragment, '/') . '/');

$driver->$method(...$args);
}

/**
* Data provider listing every BaseDriver method that must be unsupported.
*/
public static function dataProviderUnsupportedActionsThrow() {
$user = new \stdClass();
$term = new \stdClass();
$entity = new \stdClass();

return [
'getRandom' => ['getRandom', [], 'generate random'],
'userCreate' => ['userCreate', [$user], 'create users'],
'userDelete' => ['userDelete', [$user], 'delete users'],
'processBatch' => ['processBatch', [], 'process batch actions'],
'userAddRole' => ['userAddRole', [$user, 'editor'], 'add roles'],
'fetchWatchdog' => ['fetchWatchdog', [], 'access watchdog entries'],
'clearCache' => ['clearCache', [], 'clear Drupal caches'],
'clearStaticCaches' => ['clearStaticCaches', [], 'clear static caches'],
'createNode' => ['createNode', [new \stdClass()], 'create nodes'],
'nodeDelete' => ['nodeDelete', [new \stdClass()], 'delete nodes'],
'runCron' => ['runCron', [], 'run cron'],
'createTerm' => ['createTerm', [$term], 'create terms'],
'termDelete' => ['termDelete', [$term], 'delete terms'],
'roleCreate' => ['roleCreate', [[]], 'create roles'],
'roleDelete' => ['roleDelete', [1], 'delete roles'],
'configGet' => ['configGet', ['system.site', 'name'], 'config get'],
'configSet' => ['configSet', ['system.site', 'name', 'v'], 'config set'],
'createEntity' => ['createEntity', ['node', $entity], 'create entities using the generic Entity API'],
'entityDelete' => ['entityDelete', ['node', $entity], 'delete entities using the generic Entity API'],
'startCollectingMail' => ['startCollectingMail', [], 'work with mail'],
'stopCollectingMail' => ['stopCollectingMail', [], 'work with mail'],
'getMail' => ['getMail', [], 'work with mail'],
'clearMail' => ['clearMail', [], 'work with mail'],
'sendMail' => ['sendMail', ['body', 'subject', 'to', 'en'], 'work with mail'],
'moduleInstall' => ['moduleInstall', ['node'], 'install modules'],
'moduleUninstall' => ['moduleUninstall', ['node'], 'uninstall modules'],
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Drupal\Tests\Driver\Exception;

use Drupal\Driver\DriverInterface;
use Drupal\Driver\Exception\UnsupportedDriverActionException;
use PHPUnit\Framework\TestCase;

/**
* Tests the UnsupportedDriverActionException.
*/
class UnsupportedDriverActionExceptionTest extends TestCase {

/**
* Tests that the message template is populated with the driver class name.
*/
public function testMessageFormatting() {
$driver = $this->createMock(DriverInterface::class);
$driver_class = get_class($driver);

$exception = new UnsupportedDriverActionException('Action %s is not supported.', $driver);

$this->assertSame(sprintf('Action %s is not supported.', $driver_class), $exception->getMessage());
}

/**
* Tests that the driver is accessible via getDriver().
*/
public function testGetDriverReturnsConstructorArgument() {
$driver = $this->createMock(DriverInterface::class);

$exception = new UnsupportedDriverActionException('%s', $driver);

$this->assertSame($driver, $exception->getDriver());
}

/**
* Tests that code and previous exception are propagated to the parent.
*/
public function testCodeAndPreviousArePropagated() {
$driver = $this->createMock(DriverInterface::class);
$previous = new \RuntimeException('root cause');

$exception = new UnsupportedDriverActionException('%s', $driver, 42, $previous);

$this->assertSame(42, $exception->getCode());
$this->assertSame($previous, $exception->getPrevious());
}

}
187 changes: 187 additions & 0 deletions tests/Drupal/Tests/Driver/Fields/Drupal8/AddressHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

namespace Drupal\Tests\Driver\Fields\Drupal8;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Driver\Fields\Drupal8\AddressHandler;
use PHPUnit\Framework\TestCase;

/**
* Tests the AddressHandler field handler.
*/
class AddressHandlerTest extends TestCase {

/**
* Tests that a string value uses the first visible field.
*/
public function testStringValueUsesFirstVisibleField() {
$handler = $this->createHandler();

$result = $handler->expand(['Just a name']);

$this->assertSame([['given_name' => 'Just a name']], $result);
}

/**
* Tests that keyed values are preserved and defaults filled in.
*/
public function testKeyedValuesAreKeptAndDefaultCountryApplied() {
$handler = $this->createHandler();

$result = $handler->expand([
[
'given_name' => 'John',
'family_name' => 'Doe',
],
]);

$this->assertSame([
[
'given_name' => 'John',
'family_name' => 'Doe',
'country_code' => 'AU',
],
], $result);
}

/**
* Tests that numeric indices are assigned in the order of visible fields.
*/
public function testNumericIndicesMapToVisibleFieldOrder() {
$handler = $this->createHandler();

$result = $handler->expand([
['John', 'Doe'],
]);

$this->assertSame([
[
'given_name' => 'John',
'additional_name' => 'Doe',
'country_code' => 'AU',
],
], $result);
}

/**
* Tests that hidden fields are removed from the visible field list.
*/
public function testHiddenFieldsAreSkippedForNumericIndices() {
$handler = $this->createHandler([
'givenName' => ['override' => 'hidden'],
'additionalName' => ['override' => 'hidden'],
]);

$result = $handler->expand([
['Doe'],
]);

$this->assertSame([
[
'family_name' => 'Doe',
'country_code' => 'AU',
],
], $result);
}

/**
* Tests that non-hidden overrides do not alter the visible field list.
*/
public function testNonHiddenOverridesAreIgnored() {
$handler = $this->createHandler([
'givenName' => ['override' => 'optional'],
]);

$result = $handler->expand([
['John'],
]);

$this->assertSame([
[
'given_name' => 'John',
'country_code' => 'AU',
],
], $result);
}

/**
* Tests that excess numeric indices trigger an exception.
*/
public function testTooManyNumericIndicesThrows() {
$handler = $this->createHandler([
'additionalName' => ['override' => 'hidden'],
'familyName' => ['override' => 'hidden'],
'organization' => ['override' => 'hidden'],
'addressLine1' => ['override' => 'hidden'],
'addressLine2' => ['override' => 'hidden'],
'postalCode' => ['override' => 'hidden'],
'sortingCode' => ['override' => 'hidden'],
'locality' => ['override' => 'hidden'],
'administrativeArea' => ['override' => 'hidden'],
'countryCode' => ['override' => 'hidden'],
]);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Too many address sub-field values supplied; only 1 visible fields available.');

$handler->expand([
['John', 'Extra'],
]);
}

/**
* Tests that a non-numeric, unknown sub-field key throws an exception.
*/
public function testUnknownKeyThrows() {
$handler = $this->createHandler();

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid address sub-field key: unknown_key.');

$handler->expand([
['unknown_key' => 'value'],
]);
}

/**
* Tests that an explicit country_code is not overridden by the default.
*/
public function testExplicitCountryCodeIsPreserved() {
$handler = $this->createHandler();

$result = $handler->expand([
['country_code' => 'US'],
]);

$this->assertSame([['country_code' => 'US']], $result);
}

/**
* Creates an AddressHandler with an injected fieldConfig mock.
*
* @param array $field_overrides
* Address field override settings.
* @param array $available_countries
* Available countries keyed by code.
*
* @return \Drupal\Driver\Fields\Drupal8\AddressHandler
* Handler instance with fieldConfig populated.
*/
protected function createHandler(array $field_overrides = [], array $available_countries = ['AU' => 'AU']) {
$field_config = $this->createMock(FieldDefinitionInterface::class);
$field_config->method('getSettings')->willReturn([
'field_overrides' => $field_overrides,
'available_countries' => $available_countries,
]);

$reflection = new \ReflectionClass(AddressHandler::class);
$handler = $reflection->newInstanceWithoutConstructor();

$property = new \ReflectionProperty(AddressHandler::class, 'fieldConfig');
$property->setAccessible(TRUE);
$property->setValue($handler, $field_config);

return $handler;
}

}
Loading
Loading