Esempio n. 1
0
 public function testForcePrimaryAuthenticationProviders()
 {
     $mockA = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class);
     $mockB = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class);
     $mockB2 = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class);
     $mockA->expects($this->any())->method('getUniqueId')->will($this->returnValue('A'));
     $mockB->expects($this->any())->method('getUniqueId')->will($this->returnValue('B'));
     $mockB2->expects($this->any())->method('getUniqueId')->will($this->returnValue('B'));
     $this->primaryauthMocks = [$mockA];
     $this->logger = new \TestLogger(true);
     // Test without first initializing the configured providers
     $this->initializeManager();
     $this->manager->forcePrimaryAuthenticationProviders([$mockB], 'testing');
     $this->assertSame(['B' => $mockB], $this->managerPriv->getPrimaryAuthenticationProviders());
     $this->assertSame(null, $this->managerPriv->getAuthenticationProvider('A'));
     $this->assertSame($mockB, $this->managerPriv->getAuthenticationProvider('B'));
     $this->assertSame([[LogLevel::WARNING, 'Overriding AuthManager primary authn because testing']], $this->logger->getBuffer());
     $this->logger->clearBuffer();
     // Test with first initializing the configured providers
     $this->initializeManager();
     $this->assertSame($mockA, $this->managerPriv->getAuthenticationProvider('A'));
     $this->assertSame(null, $this->managerPriv->getAuthenticationProvider('B'));
     $this->request->getSession()->setSecret('AuthManager::authnState', 'test');
     $this->request->getSession()->setSecret('AuthManager::accountCreationState', 'test');
     $this->manager->forcePrimaryAuthenticationProviders([$mockB], 'testing');
     $this->assertSame(['B' => $mockB], $this->managerPriv->getPrimaryAuthenticationProviders());
     $this->assertSame(null, $this->managerPriv->getAuthenticationProvider('A'));
     $this->assertSame($mockB, $this->managerPriv->getAuthenticationProvider('B'));
     $this->assertNull($this->request->getSession()->getSecret('AuthManager::authnState'));
     $this->assertNull($this->request->getSession()->getSecret('AuthManager::accountCreationState'));
     $this->assertSame([[LogLevel::WARNING, 'Overriding AuthManager primary authn because testing'], [LogLevel::WARNING, 'PrimaryAuthenticationProviders have already been accessed! I hope nothing breaks.']], $this->logger->getBuffer());
     $this->logger->clearBuffer();
     // Test duplicate IDs
     $this->initializeManager();
     try {
         $this->manager->forcePrimaryAuthenticationProviders([$mockB, $mockB2], 'testing');
         $this->fail('Expected exception not thrown');
     } catch (\RuntimeException $ex) {
         $class1 = get_class($mockB);
         $class2 = get_class($mockB2);
         $this->assertSame("Duplicate specifications for id B (classes {$class2} and {$class1})", $ex->getMessage());
     }
     // Wrong classes
     $mock = $this->getMockForAbstractClass(AuthenticationProvider::class);
     $mock->expects($this->any())->method('getUniqueId')->will($this->returnValue('X'));
     $class = get_class($mock);
     try {
         $this->manager->forcePrimaryAuthenticationProviders([$mock], 'testing');
         $this->fail('Expected exception not thrown');
     } catch (\RuntimeException $ex) {
         $this->assertSame("Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got {$class}", $ex->getMessage());
     }
 }