Unverified Commit d7a1176d authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3565033 Fix using numeric literals for 'version' in .info.yml files, cast to strings

By: dww
By: drupalfan2
By: godotislate
By: berdir
By: bohus ulrych
By: quietone
By: samitk
By: nicxvan
By: alexpott
parent c79e2a0b
Loading
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -64,9 +64,17 @@ public function parse($filename) {
    catch (\UnexpectedValueException) {
      throw new InfoParserException("The 'core_version_requirement' constraint ({$parsed_info['core_version_requirement']}) is not a valid value in $filename");
    }
    if (isset($parsed_info['version']) && $parsed_info['version'] === 'VERSION') {
    if (isset($parsed_info['version'])) {
      if ($parsed_info['version'] === 'VERSION') {
        $parsed_info['version'] = \Drupal::VERSION;
      }
      elseif (!is_scalar($parsed_info['version'])) {
        throw new InfoParserException("The 'version' value must be a scalar in $filename");
      }
      elseif (!is_string($parsed_info['version'])) {
        $parsed_info['version'] = (string) $parsed_info['version'];
      }
    }
    $parsed_info += [ExtensionLifecycle::LIFECYCLE_IDENTIFIER => ExtensionLifecycle::STABLE];
    $lifecycle = $parsed_info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER];
    if (!ExtensionLifecycle::isValid($lifecycle)) {
+58 −0
Original line number Diff line number Diff line
@@ -119,6 +119,18 @@ public static function providerInfoException(): array {
YML,
        'Missing required keys (type, name) in',
      ],
      'version is not scalar' => [
      <<<YML
package: Core
type: module
name: Broken
core_version_requirement: '*'
version:
  - Not
  - Scalar
YML,
        "The 'version' value must be a scalar in",
      ],
    ];
  }

@@ -176,6 +188,52 @@ public function testTestingPackageMissingCoreVersionRequirement(): void {
    $this->assertSame($info_values['core_version_requirement'], \Drupal::VERSION);
  }

  /**
   * Tests a version string that looks like a float.
   *
   * @legacy-covers ::parse
   */
  public function testFloatLikeVersion(): void {
    $float_like_version = <<<VERSION_TEST
core_version_requirement: '*'
name: 'Float-like version'
type: module
version: '1.0'
VERSION_TEST;

    vfsStream::setup('modules');
    vfsStream::create([
      'fixtures' => [
        'float_like_version.info.txt' => $float_like_version,
      ],
    ]);
    $info_values = $this->infoParser->parse(vfsStream::url('modules/fixtures/float_like_version.info.txt'));
    $this->assertSame('1.0', $info_values['version'], 'Version that looks like a float should be a string');
  }

  /**
   * Tests a version string that is a float.
   *
   * @legacy-covers ::parse
   */
  public function testFloatVersion(): void {
    $float_version = <<<VERSION_TEST
core_version_requirement: '*'
name: 'Float version'
type: module
version: 1.1
VERSION_TEST;

    vfsStream::setup('modules');
    vfsStream::create([
      'fixtures' => [
        'float_version.info.txt' => $float_version,
      ],
    ]);
    $info_values = $this->infoParser->parse(vfsStream::url('modules/fixtures/float_version.info.txt'));
    $this->assertSame('1.1', $info_values['version'], 'Floating point version should be cast to a string');
  }

  /**
   * Tests common info file.
   *