Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
HTTPFederatedPreauthUserDetailsProviderTest.php 3.80 KiB
<?php

use edu\wisc\doit\uwphps\UserDetailsProvider;
use edu\wisc\doit\uwphps\preauth\FederatedPreauthUserDetailsProvider;

/**
 * Tests for {@link FederatedPreauthUserDetailsProvider}.
 */
class HTTPFederatedPreauthUserDetailsProviderTest extends \PHPUnit_Framework_TestCase
{

    /** @var array */
    private $attributes;

    /** @var UserDetailsProvider */
    private $userProvider;

    /**
     * Populate $_SERVER with Shib attributes to simulate a logged in user
     */
    protected function setUp()
    {
        $jsonString = file_get_contents(__DIR__ . "/../../../../../resources/testuser_http.json");
        if ($jsonString === false) {
            return null;
        }

        $this->attributes = json_decode($jsonString, true);
        $this->userProvider = new FederatedPreauthUserDetailsProvider(true);

        putenv($this->mapAttribute(UserDetailsProvider::EPPN) . '=' . $this->attributes[$this->mapAttribute(UserDetailsProvider::EPPN)]);
        putenv($this->mapAttribute(FederatedPreauthUserDetailsProvider::SPVI) . '=' . $this->attributes[$this->mapAttribute(FederatedPreauthUserDetailsProvider::SPVI)]);
        putenv($this->mapAttribute(FederatedPreauthUserDetailsProvider::FULL_NAME) . '=' . $this->attributes[$this->mapAttribute(FederatedPreauthUserDetailsProvider::FULL_NAME)]);
        putenv($this->mapAttribute(FederatedPreauthUserDetailsProvider::FIRST_NAME) . '=' . $this->attributes[$this->mapAttribute(FederatedPreauthUserDetailsProvider::FIRST_NAME)]);
        putenv($this->mapAttribute(FederatedPreauthUserDetailsProvider::LAST_NAME) . '=' . $this->attributes[$this->mapAttribute(FederatedPreauthUserDetailsProvider::LAST_NAME)]);
        putenv($this->mapAttribute(UserDetailsProvider::UDDS) . '=' . $this->attributes[$this->mapAttribute(UserDetailsProvider::UDDS)]);
        putenv($this->mapAttribute(FederatedPreauthUserDetailsProvider::EMAIL) . '=' . $this->attributes[$this->mapAttribute(FederatedPreauthUserDetailsProvider::EMAIL)]);
        putenv($this->mapAttribute(UserDetailsProvider::SOURCE) . '=' . $this->attributes[$this->mapAttribute(UserDetailsProvider::SOURCE)]);
        putenv($this->mapAttribute(UserDetailsProvider::ISIS_EMPLID) . '=' . $this->attributes[$this->mapAttribute(UserDetailsProvider::ISIS_EMPLID)]);
        putenv(UserDetailsProvider::SHIB_SESSION_ID_HTTP  . '=' . $this->attributes[UserDetailsProvider::SHIB_SESSION_ID_HTTP]);
    }

    /**
     * @test
     */
    public function loadUser()
    {
        $user = $this->userProvider->loadUser();
        $this->assertNotNull($user);
        $this->assertEquals("bbadger@wisc.edu", $user->getEppn());
        $this->assertEquals("UW123A456", $user->getPvi());
        $this->assertEquals("BUCKINGHAM BADGER", $user->getFullName());
        $this->assertEquals("bucky.badger@wisc.edu", $user->getEmailAddress());
        $this->assertEquals("a_source", $user->getSource());
        $this->assertEquals("123456789", $user->getIsisEmplid());
        $this->assertEquals("BUCKINGHAM", $user->getFirstName());
        $this->assertEquals("BADGER", $user->getLastName());
        $this->assertEquals(["A061234", "A072345"], $user->getUddsMembership());
    }

    /**
     * @test
     */
    public function loadUserWithNoEPPN()
    {
        // Clear Shib session ID to simulate no session
        putenv(UserDetailsProvider::SHIB_SESSION_ID_HTTP);
        $user = $this->userProvider->loadUser();
        $this->assertNull($user);
    }

    /**
     * @test
     */
    public function loadUserWithNoEmail()
    {
        // Clear email to simulate no email
        putenv('HTTP_' . strtoupper(FederatedPreauthUserDetailsProvider::EMAIL));
        $user = $this->userProvider->loadUser();
        $this->assertFalse($user->getEmailAddress());
    }

    private function mapAttribute($attribute)
    {
        return 'HTTP_' . strtoupper($attribute);
    }

}