addPsr4('.var_export($namespace.'\\', true).', __DIR__."/../dependency"); file_put_contents('.var_export($counter, true).', "x", FILE_APPEND); return $loader;'); File::put($directory.'/bad/vendor/autoload.php', ' $id, 'name' => $id, 'version' => '1.0.0'], JSON_THROW_ON_ERROR)); } $validator = new ExtensionManifestValidator; $manifests = collect(['bad' => $validator->fromDirectory($directory.'/bad'), 'good' => $validator->fromDirectory($directory.'/good')]); $repository = Mockery::mock(ExtensionRepository::class); $repository->shouldReceive('recordFailure')->twice()->with('bad', Mockery::type('string'), Mockery::type(Throwable::class), 'register'); $repository->shouldReceive('clearErrors')->twice()->with(['good']); try { $loader = new ExtensionProviderLoader($this->app, $repository); $loader->registerProviders($manifests); $loader->registerProviders($manifests); expect(class_exists($namespace.'\\Included'))->toBeTrue(); expect(File::get($counter))->toBe('x'); } finally { File::deleteDirectory($directory); } }); test('a bundled autoloader that shares its Composer suffix with a loaded one fails alone', function (): void { $directory = sys_get_temp_dir().'/ptero-suffix-'.uniqid(); $suffix = 'Probe'.str_replace('.', '', uniqid('', true)); foreach (['first', 'second'] as $id) { // What Composer generates: autoload.php declares a class named after the suffix. File::ensureDirectoryExists($directory.'/'.$id.'/vendor/composer'); File::put($directory.'/'.$id.'/vendor/autoload.php', ' $id, 'name' => $id, 'version' => '1.0.0'], JSON_THROW_ON_ERROR)); } $validator = new ExtensionManifestValidator; $manifests = collect(['first' => $validator->fromDirectory($directory.'/first'), 'second' => $validator->fromDirectory($directory.'/second')]); $repository = Mockery::mock(ExtensionRepository::class); $repository->shouldReceive('recordFailure')->once()->with('second', Mockery::on(fn (string $reason): bool => str_contains($reason, 'ComposerAutoloaderInit'.$suffix.', which another package already loaded')), Mockery::type(Throwable::class), 'register'); $repository->shouldReceive('clearErrors')->once()->with(['first']); try { (new ExtensionProviderLoader($this->app, $repository))->registerProviders($manifests); } finally { File::deleteDirectory($directory); } }); test('the migrator knows the migrations of every extension that loads', function (): void { $directory = sys_get_temp_dir().'/ptero-migrations-'.uniqid(); foreach (['plain', 'broken'] as $id) { File::ensureDirectoryExists($directory.'/'.$id.'/database/migrations'); File::put($directory.'/'.$id.'/extension.json', json_encode(['id' => $id, 'name' => $id, 'version' => '1.0.0', 'provider' => $id === 'broken' ? 'Missing\\Provider' : null], JSON_THROW_ON_ERROR)); } $validator = new ExtensionManifestValidator; $repository = Mockery::mock(ExtensionRepository::class); $repository->shouldReceive('recordFailure')->once()->with('broken', Mockery::type('string'), Mockery::type(Throwable::class), 'register'); $repository->shouldReceive('clearErrors')->once()->with(['plain']); try { (new ExtensionProviderLoader($this->app, $repository))->registerProviders(collect(['plain' => $validator->fromDirectory($directory.'/plain'), 'broken' => $validator->fromDirectory($directory.'/broken')])); $paths = $this->app->make(Migrator::class)->paths(); expect($paths)->toContain($directory.'/plain'.DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'migrations'); expect($paths)->not->toContain($directory.'/broken'.DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'migrations'); } finally { File::deleteDirectory($directory); } }); test('extension class loaders never replace a class the panel loads', function (): void { $directory = sys_get_temp_dir().'/ptero-shadow-'.uniqid(); $decoy = 'addPsr4("Pterodactyl\\\\", __DIR__."/../bundled"); $loader->register(true); return $loader;'); // A manifest that skipped validation, claiming the panel namespace for itself. $manifest = ExtensionManifest::fromValidatedData($directory, ['id' => 'shadow', 'name' => 'Shadow', 'version' => '1.0.0'], ['Pterodactyl\\' => 'src'], null, 'native', null); $repository = Mockery::mock(ExtensionRepository::class); $repository->shouldReceive('clearErrors')->once()->with(['shadow']); try { (new ExtensionProviderLoader($this->app, $repository))->registerProviders(collect(['shadow' => $manifest])); expect((new ReflectionClass(HasActiveNodesException::class))->getFileName())->toBe(realpath(app_path('Exceptions/Service/Location/HasActiveNodesException.php'))); } finally { File::deleteDirectory($directory); } });