class ProjectReleaseTest

Same name and namespace in other branches
  1. 9 core/modules/update/tests/src/Unit/ProjectReleaseTest.php \Drupal\Tests\update\Unit\ProjectReleaseTest
  2. 10 core/modules/update/tests/src/Unit/ProjectReleaseTest.php \Drupal\Tests\update\Unit\ProjectReleaseTest

@coversDefaultClass \Drupal\update\ProjectRelease

@group update

Hierarchy

Expanded class hierarchy of ProjectReleaseTest

File

core/modules/update/tests/src/Unit/ProjectReleaseTest.php, line 15

Namespace

Drupal\Tests\update\Unit
View source
class ProjectReleaseTest extends UnitTestCase {
    
    /**
     * Tests creating with valid data.
     *
     * @param mixed[] $data
     *   The data to test. It will be combined with ::getValidData() results.
     * @param mixed[] $expected
     *   The values expected to be returned from the object methods.
     *
     * @covers ::createFromArray
     * @covers ::isInsecure
     * @covers ::isSecurityRelease
     * @covers ::isPublished
     * @covers ::isUnsupported
     * @covers ::isUnsupported
     *
     * @dataProvider providerCreateFromArray
     */
    public function testCreateFromArray(array $data, array $expected = []) : void {
        $data += $this->getValidData();
        $expected += $data;
        // If not set provide default values that match ::getValidData().
        $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());
    }
    
    /**
     * Data provider for testCreateFromArray().
     *
     * @return mixed
     *   Test cases for testCreateFromArray().
     */
    public static 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,
                ],
            ],
        ];
    }
    
    /**
     * Tests that optional fields can be omitted.
     *
     * @covers ::createFromArray
     */
    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());
        // Confirm that all getters that rely on 'terms' default to FALSE.
        $this->assertFalse($release->isSecurityRelease());
        $this->assertFalse($release->isInsecure());
        $this->assertFalse($release->isUnsupported());
    }
    
    /**
     * Tests exceptions with missing fields.
     *
     * @param string $missing_field
     *   The field to test.
     *
     * @covers ::createFromArray
     *
     * @dataProvider providerCreateFromArrayMissingField
     */
    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);
    }
    
    /**
     * Data provider for testCreateFromArrayMissingField().
     */
    public static function providerCreateFromArrayMissingField() : array {
        return [
            'status' => [
                'status',
            ],
            'version' => [
                'version',
            ],
            'release_link' => [
                'release_link',
            ],
        ];
    }
    
    /**
     * Tests exceptions for invalid field types.
     *
     * @param string $invalid_field
     *   The field to test for an invalid value.
     * @param mixed $invalid_value
     *   The invalid value to use in the field.
     * @param string $expected_message
     *   The expected message for the field.
     *
     * @covers ::createFromArray
     *
     * @dataProvider providerCreateFromArrayInvalidField
     */
    public function testCreateFromArrayInvalidField(string $invalid_field, $invalid_value, string $expected_message) : void {
        $data = $this->getValidData();
        // Set the field a value that is not valid for any of the fields in the
        // feed.
        $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);
    }
    
    /**
     * Data provider for testCreateFromArrayInvalidField().
     */
    public static 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.',
            ],
        ];
    }
    
    /**
     * Gets valid data for a project release.
     *
     * @return mixed[]
     *   The data for the project release.
     */
    protected function getValidData() : array {
        return [
            'status' => 'published',
            'release_link' => 'https://example.com/release-link',
            'version' => '8.0.0',
            'download_link' => 'https://example.com/download-link',
            'core_compatibility_message' => 'This is compatible',
            'date' => 1452229200,
            'terms' => [
                'Release type' => [
                    'Security update',
                    'Unsupported',
                    'Insecure',
                ],
            ],
            'core_compatible' => TRUE,
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
ProjectReleaseTest::getValidData protected function Gets valid data for a project release.
ProjectReleaseTest::providerCreateFromArray public static function Data provider for testCreateFromArray().
ProjectReleaseTest::providerCreateFromArrayInvalidField public static function Data provider for testCreateFromArrayInvalidField().
ProjectReleaseTest::providerCreateFromArrayMissingField public static function Data provider for testCreateFromArrayMissingField().
ProjectReleaseTest::testCreateFromArray public function Tests creating with valid data.
ProjectReleaseTest::testCreateFromArrayInvalidField public function Tests exceptions for invalid field types.
ProjectReleaseTest::testCreateFromArrayMissingField public function Tests exceptions with missing fields.
ProjectReleaseTest::testOptionalFields public function Tests that optional fields can be omitted.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 354
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.