View source
<?php
namespace Drupal\Tests\system\Unit\SecurityAdvisories;
use Drupal\Tests\UnitTestCase;
use Drupal\system\SecurityAdvisories\SecurityAdvisory;
class SecurityAdvisoryTest extends UnitTestCase {
public function testCreateFromArray(array $changes, array $expected = []) : void {
$data = $changes;
$data += $this
->getValidData();
$expected += $data;
$sa = SecurityAdvisory::createFromArray($data);
$this
->assertInstanceOf(SecurityAdvisory::class, $sa);
$this
->assertSame($expected['title'], $sa
->getTitle());
$this
->assertSame($expected['project'], $sa
->getProject());
$this
->assertSame($expected['type'], $sa
->getProjectType());
$this
->assertSame($expected['link'], $sa
->getUrl());
$this
->assertSame($expected['insecure'], $sa
->getInsecureVersions());
$this
->assertSame($expected['is_psa'], $sa
->isPsa());
$this
->assertSame($expected['type'] === 'core', $sa
->isCoreAdvisory());
}
public function providerCreateFromArray() : array {
return [
[
[
'is_psa' => 1,
],
[
'is_psa' => TRUE,
],
],
[
[
'is_psa' => '1',
],
[
'is_psa' => TRUE,
],
],
[
[
'is_psa' => TRUE,
],
[
'is_psa' => TRUE,
],
],
[
[
'is_psa' => 0,
],
[
'is_psa' => FALSE,
],
],
[
[
'is_psa' => '0',
],
[
'is_psa' => FALSE,
],
],
[
[
'is_psa' => FALSE,
],
[
'is_psa' => FALSE,
],
],
[
[
'type' => 'module',
],
],
[
[
'type' => 'theme',
],
],
[
[
'type' => 'core',
],
],
];
}
public function testCreateFromArrayMissingField(string $missing_field) : void {
$data = $this
->getValidData();
unset($data[$missing_field]);
$this
->expectException(\UnexpectedValueException::class);
$expected_message = 'Malformed security advisory:.*' . preg_quote("[{$missing_field}]:", '/');
$expected_message .= '.*This field is missing';
$this
->expectExceptionMessageMatches("/{$expected_message}/s");
SecurityAdvisory::createFromArray($data);
}
public function providerCreateFromArrayMissingField() : array {
return [
'title' => [
'title',
],
'link' => [
'link',
],
'project' => [
'project',
],
'type' => [
'type',
],
'is_psa' => [
'is_psa',
],
'insecure' => [
'insecure',
],
];
}
public function testCreateFromArrayInvalidField(string $invalid_field, string $expected_type_message) : void {
$data = $this
->getValidData();
$data[$invalid_field] = new \stdClass();
$this
->expectException(\UnexpectedValueException::class);
$expected_message = 'Malformed security advisory:.*' . preg_quote("[{$invalid_field}]:", '/');
$expected_message .= ".*{$expected_type_message}";
$this
->expectExceptionMessageMatches("/{$expected_message}/s");
SecurityAdvisory::createFromArray($data);
}
public function providerCreateFromArrayInvalidField() : array {
return [
'title' => [
'title',
'This value should be of type string.',
],
'link' => [
'link',
'This value should be of type string.',
],
'project' => [
'project',
'This value should be of type string.',
],
'type' => [
'type',
'This value should be of type string.',
],
'is_psa' => [
'is_psa',
'The value you selected is not a valid choice.',
],
'insecure' => [
'insecure',
'This value should be of type array.',
],
];
}
protected function getValidData() : array {
return [
'title' => 'Generic Module1 Test - Moderately critical - Access bypass - SA-CONTRIB-2019-02-02',
'link' => 'https://www.drupal.org/SA-CONTRIB-2019-02-02',
'project' => 'generic_module1_test',
'type' => 'module',
'is_psa' => FALSE,
'insecure' => [
'8.x-1.1',
],
'pubDate' => 'Tue, 19 Mar 2019 12 => 50 => 00 +0000',
'unknown_field' => 'ignored value',
];
}
}