class ProjectReleaseTest

Same name in other branches
  1. 10 core/modules/update/tests/src/Unit/ProjectReleaseTest.php \Drupal\Tests\update\Unit\ProjectReleaseTest
  2. 11.x 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 13

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 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 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 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://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,
        ];
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
ProjectReleaseTest::getValidData protected function Gets valid data for a project release.
ProjectReleaseTest::providerCreateFromArray public function Data provider for testCreateFromArray().
ProjectReleaseTest::providerCreateFromArrayInvalidField public function Data provider for testCreateFromArrayInvalidField().
ProjectReleaseTest::providerCreateFromArrayMissingField public 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.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 338
UnitTestCase::setUpBeforeClass public static function

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