View source
<?php
namespace Drupal\Tests\update\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\update\ProjectRelease;
class ProjectReleaseTest extends UnitTestCase {
public function testCreateFromArray(array $data, array $expected = []) : void {
$data += $this
->getValidData();
$expected += $data;
$expected += [
'is_published' => TRUE,
'is_unsupported' => TRUE,
'is_security_release' => TRUE,
'is_insecure' => TRUE,
];
$release = ProjectRelease::createFromArray($data);
$this
->assertInstanceOf(ProjectRelease::class, $release);
$this
->assertSame($expected['version'], $release
->getVersion());
$this
->assertSame($expected['date'], $release
->getDate());
$this
->assertSame($expected['download_link'], $release
->getDownloadUrl());
$this
->assertSame($expected['release_link'], $release
->getReleaseUrl());
$this
->assertSame($expected['core_compatibility_message'], $release
->getCoreCompatibilityMessage());
$this
->assertSame($expected['core_compatible'], $release
->isCoreCompatible());
$this
->assertSame($expected['is_published'], $release
->isPublished());
$this
->assertSame($expected['is_unsupported'], $release
->isUnsupported());
$this
->assertSame($expected['is_security_release'], $release
->isSecurityRelease());
$this
->assertSame($expected['is_insecure'], $release
->isInsecure());
}
public function providerCreateFromArray() : array {
return [
'default valid' => [
'data' => [],
],
'valid with extra field' => [
'data' => [
'extra' => 'This value is ignored and will not trigger a validation error.',
],
],
'no release types' => [
'data' => [
'terms' => [
'Release type' => [],
],
],
'expected' => [
'is_unsupported' => FALSE,
'is_security_release' => FALSE,
'is_insecure' => FALSE,
],
],
'unpublished' => [
'data' => [
'status' => 'unpublished',
],
'expected' => [
'is_published' => FALSE,
],
],
'core_compatible false' => [
'data' => [
'core_compatible' => FALSE,
],
],
'core_compatible NULL' => [
'data' => [
'core_compatible' => NULL,
],
],
];
}
public function testOptionalFields() : void {
$data = $this
->getValidData();
unset($data['core_compatible'], $data['core_compatibility_message'], $data['download_link'], $data['date'], $data['terms']);
$release = ProjectRelease::createFromArray($data);
$this
->assertNull($release
->isCoreCompatible());
$this
->assertNull($release
->getCoreCompatibilityMessage());
$this
->assertNull($release
->getDate());
$this
->assertFalse($release
->isSecurityRelease());
$this
->assertFalse($release
->isInsecure());
$this
->assertFalse($release
->isUnsupported());
}
public function testCreateFromArrayMissingField(string $missing_field) : void {
$data = $this
->getValidData();
unset($data[$missing_field]);
$this
->expectException(\UnexpectedValueException::class);
$expected_message = 'Malformed release data:.*' . preg_quote("[{$missing_field}]:", '/');
$expected_message .= '.*This field is missing';
$this
->expectExceptionMessageMatches("/{$expected_message}/s");
ProjectRelease::createFromArray($data);
}
public function providerCreateFromArrayMissingField() : array {
return [
'status' => [
'status',
],
'version' => [
'version',
],
'release_link' => [
'release_link',
],
];
}
public function testCreateFromArrayInvalidField(string $invalid_field, $invalid_value, string $expected_message) : void {
$data = $this
->getValidData();
$data[$invalid_field] = $invalid_value;
$this
->expectException(\UnexpectedValueException::class);
$expected_exception_message = 'Malformed release data:.*' . preg_quote("[{$invalid_field}]:", '/');
$expected_exception_message .= ".*{$expected_message}";
$this
->expectExceptionMessageMatches("/{$expected_exception_message}/s");
ProjectRelease::createFromArray($data);
}
public function providerCreateFromArrayInvalidField() : array {
return [
'status other' => [
'invalid_field' => 'status',
'invalid_value' => 'other',
'expected_message' => 'The value you selected is not a valid choice.',
],
'status non-string' => [
'invalid_field' => 'status',
'invalid_value' => new \stdClass(),
'expected_message' => 'The value you selected is not a valid choice.',
],
'terms non-array' => [
'invalid_field' => 'terms',
'invalid_value' => 'Unsupported',
'expected_message' => 'This value should be of type array.',
],
'version blank' => [
'invalid_field' => 'version',
'invalid_value' => '',
'expected_message' => 'This value should not be blank.',
],
'core_compatibility_message blank' => [
'invalid_field' => 'core_compatibility_message',
'invalid_value' => '',
'expected_message' => 'This value should not be blank.',
],
'download_link blank' => [
'invalid_field' => 'download_link',
'invalid_value' => '',
'expected_message' => 'This value should not be blank.',
],
'release_link blank' => [
'invalid_field' => 'release_link',
'invalid_value' => '',
'expected_message' => 'This value should not be blank.',
],
'date non-numeric' => [
'invalid_field' => 'date',
'invalid_value' => '2 weeks ago',
'expected_message' => 'This value should be of type numeric.',
],
'core_compatible string' => [
'invalid_field' => 'core_compatible',
'invalid_value' => 'Nope',
'expected_message' => 'This value should be of type boolean.',
],
];
}
protected function getValidData() : array {
return [
'status' => 'published',
'release_link' => 'https://drupal.org/release-link',
'version' => '8.0.0',
'download_link' => 'https://drupal.org/download-link',
'core_compatibility_message' => 'This is compatible',
'date' => 1452229200,
'terms' => [
'Release type' => [
'Security update',
'Unsupported',
'Insecure',
],
],
'core_compatible' => TRUE,
];
}
}