Accedi
IT
games/xencenter-xenadmin-xcp-jumphp / tests / LocalNetworkTest.php · commit fb1b7c9
<?phpnamespace Tests;use App\Form\AddServerForm;use App\Form\MainForm;use App\Model\Connection;use App\Model\LocalNetwork;use App\Model\ServerProfile;use JumPHP\Kernel\Engine;use JumPHP\Net\RuntimeLink;use JumPHP\Runtime\ManualRuntime;use JumPHP\Runtime\RuntimeGuide;use JumPHP\Testing\GameTestCase;/** A local server (192.168.x.x) in the browser: the JumPHP runtime is required, the app says so before trying. */final class LocalNetworkTest extends GameTestCase{    private ?RuntimeLink $before = null;    protected function tearDown(): void    {        LocalNetwork::$wasm = null;        if ($this->before !== null) {            Engine::get()->setRuntime($this->before);        }        parent::tearDown();    }    /** The window with no server, as in a browser with the given runtime. */    private function browser(ManualRuntime $runtime): MainForm    {        Engine::get()->storage->delete('servers.json');        $this->game->start(MainForm::class);        /** @var MainForm $form */        $form = $this->game->scene();        $form->session->reset();        $this->game->start(MainForm::class);        $this->before = Engine::get()->runtime();        Engine::get()->setRuntime($runtime);        LocalNetwork::$wasm = true;        return $form;    }    private function dialog(MainForm $form): AddServerForm    {        $form->command('server.add');        $this->game->run(1);        $dialog = $this->game->scene();        self::assertInstanceOf(AddServerForm::class, $dialog);        return $dialog;    }    public function testALocalAddressWithoutTheRuntimeExplainsItIsRequiredAndAddIsRefused(): void    {        $runtime = new ManualRuntime(RuntimeLink::MISSING);        $dialog = $this->dialog($this->browser($runtime));        self::assertFalse($dialog->localPanel->visible, 'nothing to say before an address');        $dialog->host->text = '192.168.1.10';        $dialog->password->text = 'secret';        $this->game->run(2);        self::assertTrue($dialog->localPanel->visible, 'shown as soon as the address is typed');        self::assertStringContainsString('Install and start the JumPHP runtime', $dialog->localWarning->text);        self::assertSame('Install the JumPHP runtime…', $dialog->localFix->text);        $this->game->shot('local-network-required');        $dialog->click($dialog->add);        self::assertSame($dialog, $this->game->scene(), 'Add is refused');        self::assertStringContainsString('the JumPHP runtime is required', $dialog->error->text);        // the button opens the runtime's guide        $dialog->click($dialog->localFix);        $this->game->run(1);        self::assertInstanceOf(RuntimeGuide::class, $this->game->scene());        $this->game->scene()->close();        $this->game->run(1);        // a public address is not concerned        $dialog->host->text = 'xcp.example.com';        $this->game->run(1);        self::assertFalse($dialog->localPanel->visible);    }    public function testOnceTheRuntimeIsAllowedTheLocalAddressIsAccepted(): void    {        $runtime = new ManualRuntime(RuntimeLink::RUNNING);        $dialog = $this->dialog($this->browser($runtime));        $dialog->host->text = '10.0.0.5';        $this->game->run(1);        self::assertSame('Allow this app on my network', $dialog->localFix->text);        $dialog->click($dialog->localFix);           // pair(): the runtime says yes        $this->game->run(1);        self::assertSame([['xencenter-xenadmin-xcp-jumphp', Engine::get()->config->title()]], $runtime->pairs);        self::assertFalse($dialog->localPanel->visible, 'the local network is on now');    }    public function testASavedLocalServerFailsWithTheExplanationInsteadOfABridgeError(): void    {        $form = $this->browser(new ManualRuntime(RuntimeLink::MISSING));        $form->connect($form->session->add(new ServerProfile('192.168.1.20', 'root', 'secret', 'http')));        $this->game->run(5);        $c = $form->session->connections[0];        self::assertSame(Connection::FAILED, $c->state);        self::assertStringContainsString("'192.168.1.20' is an address of your local network", $c->error);        self::assertStringContainsString('click the red badge at the top right', $c->error);        $this->game->shot('local-network-connection');    }}