public function testProviderCreation() { $mocks = ['pre' => $this->getMockForAbstractClass(PreAuthenticationProvider::class), 'primary' => $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class), 'secondary' => $this->getMockForAbstractClass(SecondaryAuthenticationProvider::class)]; foreach ($mocks as $key => $mock) { $mock->expects($this->any())->method('getUniqueId')->will($this->returnValue($key)); $mock->expects($this->once())->method('setLogger'); $mock->expects($this->once())->method('setManager'); $mock->expects($this->once())->method('setConfig'); } $this->preauthMocks = [$mocks['pre']]; $this->primaryauthMocks = [$mocks['primary']]; $this->secondaryauthMocks = [$mocks['secondary']]; // Normal operation $this->initializeManager(); $this->assertSame($mocks['primary'], $this->managerPriv->getAuthenticationProvider('primary')); $this->assertSame($mocks['secondary'], $this->managerPriv->getAuthenticationProvider('secondary')); $this->assertSame($mocks['pre'], $this->managerPriv->getAuthenticationProvider('pre')); $this->assertSame(['pre' => $mocks['pre']], $this->managerPriv->getPreAuthenticationProviders()); $this->assertSame(['primary' => $mocks['primary']], $this->managerPriv->getPrimaryAuthenticationProviders()); $this->assertSame(['secondary' => $mocks['secondary']], $this->managerPriv->getSecondaryAuthenticationProviders()); // Duplicate IDs $mock1 = $this->getMockForAbstractClass(PreAuthenticationProvider::class); $mock2 = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class); $mock1->expects($this->any())->method('getUniqueId')->will($this->returnValue('X')); $mock2->expects($this->any())->method('getUniqueId')->will($this->returnValue('X')); $this->preauthMocks = [$mock1]; $this->primaryauthMocks = [$mock2]; $this->secondaryauthMocks = []; $this->initializeManager(true); try { $this->managerPriv->getAuthenticationProvider('Y'); $this->fail('Expected exception not thrown'); } catch (\RuntimeException $ex) { $class1 = get_class($mock1); $class2 = get_class($mock2); $this->assertSame("Duplicate specifications for id X (classes {$class1} and {$class2})", $ex->getMessage()); } // Wrong classes $mock = $this->getMockForAbstractClass(AuthenticationProvider::class); $mock->expects($this->any())->method('getUniqueId')->will($this->returnValue('X')); $class = get_class($mock); $this->preauthMocks = [$mock]; $this->primaryauthMocks = [$mock]; $this->secondaryauthMocks = [$mock]; $this->initializeManager(true); try { $this->managerPriv->getPreAuthenticationProviders(); $this->fail('Expected exception not thrown'); } catch (\RuntimeException $ex) { $this->assertSame("Expected instance of MediaWiki\\Auth\\PreAuthenticationProvider, got {$class}", $ex->getMessage()); } try { $this->managerPriv->getPrimaryAuthenticationProviders(); $this->fail('Expected exception not thrown'); } catch (\RuntimeException $ex) { $this->assertSame("Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got {$class}", $ex->getMessage()); } try { $this->managerPriv->getSecondaryAuthenticationProviders(); $this->fail('Expected exception not thrown'); } catch (\RuntimeException $ex) { $this->assertSame("Expected instance of MediaWiki\\Auth\\SecondaryAuthenticationProvider, got {$class}", $ex->getMessage()); } // Sorting $mock1 = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class); $mock2 = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class); $mock3 = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class); $mock1->expects($this->any())->method('getUniqueId')->will($this->returnValue('A')); $mock2->expects($this->any())->method('getUniqueId')->will($this->returnValue('B')); $mock3->expects($this->any())->method('getUniqueId')->will($this->returnValue('C')); $this->preauthMocks = []; $this->primaryauthMocks = [$mock1, $mock2, $mock3]; $this->secondaryauthMocks = []; $this->initializeConfig(); $config = $this->config->get('AuthManagerConfig'); $this->initializeManager(false); $this->assertSame(['A' => $mock1, 'B' => $mock2, 'C' => $mock3], $this->managerPriv->getPrimaryAuthenticationProviders(), 'sanity check'); $config['primaryauth']['A']['sort'] = 100; $config['primaryauth']['C']['sort'] = -1; $this->config->set('AuthManagerConfig', $config); $this->initializeManager(false); $this->assertSame(['C' => $mock3, 'B' => $mock2, 'A' => $mock1], $this->managerPriv->getPrimaryAuthenticationProviders()); }