set(ApplicationService::SERVICE_ID, $this->createApplicationServiceTestDouble()); $config->set(PersistenceManager::SERVICE_ID, $this->createPersistenceManagerTestDouble()); $config->set(TokenService::SERVICE_ID, $this->createTokenServiceTestDouble()); $config->set(common_cache_Cache::SERVICE_ID, $this->createCacheTestDouble()); $config->set(SessionService::SERVICE_ID, $this->createSessionServiceDouble()); ServiceManager::setServiceManager(new ServiceManager($config)); $this->initPersistence(); } /** * @noinspection PhpUnhandledExceptionInspection */ public function initPersistence(): void { /** @var \common_persistence_AdvKeyValuePersistence $persistence */ $persistence = ServiceManager::getServiceManager() ->get(PersistenceManager::SERVICE_ID) ->getPersistenceById(self::PERSISTENCE_KEY); $persistence->hSet( self::TEST_USER_SESSION_ID . '_tao_tokens', 'form_token', json_encode( [ Token::TOKEN_KEY => self::TOKEN_VALUE, Token::TIMESTAMP_KEY => microtime(true), ] ) ); } /** * @dataProvider dataProvider * * @param string $expected * @param array $options * @param tao_helpers_form_FormElement ...$elements * * @throws common_Exception */ public function testFormRender( string $expected, array $options = [], tao_helpers_form_FormElement ...$elements ): void { $sut = new FormContainerStub([], $options, ...$elements); $this->assertXmlStringEqualsXmlString( $expected, $sut ->getForm() ->render() ); } public function dataProvider(): array { $token = self::TOKEN_VALUE; return [ 'Simple form' => [ 'expected' => <<
HTML ], 'UI input form' => [ <<
HTML , 'options' => [ FormContainerStub::CSRF_PROTECTION_OPTION => true, ], new tao_helpers_form_elements_xhtml_Textbox('test'), ], 'CSRF form' => [ <<
HTML , 'options' => [ FormContainerStub::CSRF_PROTECTION_OPTION => true, ], ], 'Disabled form' => [ <<
HTML , 'options' => [ FormContainerStub::IS_DISABLED => true, ], ], 'Disabled CSRF form' => [ <<
HTML , 'options' => [ FormContainerStub::CSRF_PROTECTION_OPTION => true, FormContainerStub::IS_DISABLED => true, ], ], 'Disabled CSRF form with UI inputs' => [ <<
HTML , 'options' => [ FormContainerStub::CSRF_PROTECTION_OPTION => true, FormContainerStub::IS_DISABLED => true, ], new tao_helpers_form_elements_xhtml_Textbox('test'), ], ]; } private function createApplicationServiceTestDouble(): ApplicationService { $applicationServiceMock = $this->createMock(ApplicationService::class); $applicationServiceMock->method('getDefaultEncoding') ->willReturn('UTF-8'); return $applicationServiceMock; } private function createPersistenceManagerTestDouble(): PersistenceManager { return new PersistenceManager( [ PersistenceManager::OPTION_PERSISTENCES => [ self::PERSISTENCE_KEY => [ 'driver' => 'no_storage_adv', ], ], ] ); } private function createTokenServiceTestDouble(): TokenService { $service = new TokenService( [ TokenService::OPTION_STORE => $this->createTokenStoreTestDouble(), ] ); $service->setLogger(new NullLogger()); return $service; } private function createTokenStoreTestDouble(): TokenStore { return new TokenStoreKeyValue( [ TokenStoreKeyValue::OPTION_PERSISTENCE => self::PERSISTENCE_KEY, ] ); } private function createCacheTestDouble(): common_cache_Cache { $cacheMock = $this ->getMockBuilder(common_cache_Cache::class) ->getMock(); $cacheMock ->method('get') ->with(WidgetRegistry::CACHE_KEY) ->willReturn([]); return $cacheMock; } private function createSessionServiceDouble(): SessionService { $userMock = $this->createMock(User::class); $userMock->method('getIdentifier') ->willReturn(self::TEST_USER_SESSION_ID); $sessionServiceMock = $this->createMock(SessionService::class); $sessionServiceMock->method('getCurrentUser') ->willReturn($userMock); return $sessionServiceMock; } }