Debian Codegraph @

tests/phpunit/integration/includes/ResourceLoader/ForeignResourceStructureTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\ResourceLoader\ForeignResourceManager;
4 use MediaWiki\ResourceLoader\ForeignResourceNetworkException;
5 use PHPUnit\Framework\TestCase;
6
7 /**
8 * Verify MediaWiki core's foreign-resources.yaml.
9 *
10 * This test is under integration/ instead of structure/ because the latter
11 * also runs in CI for skin and extension repos (T203694).
12 *
13 * @coversNothing
14 */
15 class ForeignResourceStructureTest extends TestCase {
16
17 public function testVerifyIntegrity() {
18 global $IP;
19 $out = '';
20 $frm = new ForeignResourceManager(
21 "{$IP}/resources/lib/foreign-resources.yaml",
22 "{$IP}/resources/lib",
23 static function ( $text ) use ( &$out ) {
24 $out .= $text;
25 }
26 );
27
28 // The "verify" action verifies two things:
29 // 1. Mismatching SRI hashes.
30 // These throw an exception with the actual/expect values
31 // to place in foreign-resources.yaml.
32 // 2. Mismatching file contents.
33 // These print messages about each mismatching file,
34 // and then we add our help text afterward for how to
35 // automatically update the file resources.
36
37 $helpUpdate = '
38 To update a foreign resource, run:
39 $ php maintenance/manageForeignResources.php update <moduleName>
40 ';
41
42 try {
43 $this->assertTrue( $frm->run( 'verify', 'all' ), "$out\n$helpUpdate" );
44 } catch ( ForeignResourceNetworkException $e ) {
45 $this->markTestSkipped( 'Network error: ' . $e->getMessage() );
46 }
47
48 // Verify that the CycloneDX SBOM file is up to date. CDX serials are random, so we need
49 // to hack in the correct serial.
50 $cdxFile = $frm->getCdxFileLocation();
51 $this->assertFileExists( $cdxFile );
52 $cdxJsonString = file_get_contents( $cdxFile );
53 $serial = preg_match( '/"urn:uuid:[\\da-f\\-]+"/', $cdxJsonString, $matches );
54 $this->assertSame( 1, $serial );
55 $expectedCdx = preg_replace( '/"urn:uuid:[\\da-f\\-]+"/', $matches[0], $frm->generateCdx() );
56 $this->assertJsonStringEqualsJsonFile( $cdxFile, $expectedCdx,
57 "foreign-resources.cdx.json does not match foreign-resources.yaml, "
58 . "run `manageForeignResources.php make-cdx`" );
59 }
60 }