user_name = self::$user_name; $user->email1 = self::$user_email; self::$user_id = $user->save(); } /** * This method is called after the last test of this test class is run. * * Removes shared test resources */ public static function tearDownAfterClass() { self::$user->mark_deleted(self::$user_id); } /** * Test fetching of a user to be authenticated by different fields */ public function testFetchUser() { // test fetching user by ID $user = self::$auth->fetch_user(self::$user_id, 'id'); $this->assertEquals(self::$user_id, $user->id); // test fetching user by username $user = self::$auth->fetch_user(self::$user_name, 'user_name'); $this->assertEquals(self::$user_id, $user->id); // test fetching user by email (default case) $user = self::$auth->fetch_user(self::$user_email); $this->assertEquals(self::$user_id, $user->id); // test fetching user by unsupported field $user = self::$auth->fetch_user(self::$user_email, 'unsupported_field'); $this->assertNull($user->id); // test fetching non-existing user $user = self::$auth->fetch_user('some_wrong_key'); $this->assertNull($user->id); } /** * Test that get_nameid() method of SamlResponse is called by default */ public function testDefaultNameId() { // create a mock of SAML response $mock = $this->getResponse(); $mock->expects($this->once()) ->method('get_nameid'); // create a default SAML settings object require(get_custom_file_if_exists('modules/Users/authentication/SAMLAuthenticate/settings.php')); // expect that get_nameid() method of response is used by default self::$auth->get_user_id($mock, $settings); } /** * Test that custom XPath is used when specified in settings */ public function testCustomNameId() { $node_id = 'Bug49959Test'; // create response with custom XML $mock2 = $this->getResponse(); $mock2->xml = $this->getResponseXml($node_id); // create SAML settings object with custom name id definition require(get_custom_file_if_exists('modules/Users/authentication/SAMLAuthenticate/settings.php')); $settings->saml_settings['check']['user_name'] = '//root'; // expect that user ID is fetched from the document according to settings $result = self::$auth->get_user_id($mock2, $settings); $this->assertEquals($node_id, $result); } /** * Returns a mock of SamlResponse object * * @return SamlResponse */ protected function getResponse() { return $this->getMock('SamlResponse', array(), array(), '', false); } /** * Returns custom response XML document * * @param $node_id * @return DOMDocument */ protected function getResponseXml($node_id) { $document = new DOMDocument(); $document->loadXML('' . $node_id . ''); $root = $document->createElement('root'); $document->appendChild($root); return $document; } } /** * A SAMLAuthenticate class wrapper that makes some of its methods accessible */ class SAMLAuthenticateUserTest extends SAMLAuthenticateUser { public function fetch_user($id, $field = null) { return parent::fetch_user($id, $field); } public function get_user_id($samlresponse, $settings) { return parent::get_user_id($samlresponse, $settings); } }