<?php

namespace AuthStack\Driver\LDAP\tests\units;


use AuthStack\Driver\Collection\TwoFactorKeys;
use Buckhill\LDAP\Entry;
use mageekguy\atoum;
use function Buckhill\config;
use AuthStack\Driver\LDAP\User as LDAPUser;
use AuthStack\Driver\LDAP\LDAP as LDAPDriver;


class User extends atoum
{
    public function testUser()
    {
        $this
            ->given(config()->set('LDAPTest', $this->getConfig()))
            ->and($ldap = LDAPDriver::initialize('LDAPTest', 2))
            ->and($ldap->connect())
            ->then
                ->boolean($ldap->authenticate($this->getValidCredentials()))->isTrue()
                ->object($user = $ldap->getUser())->isInstanceOf(LDAPUser::class)
                ->object($user->getKeyCollection())->isInstanceOf(TwoFactorKeys::class)
                ->string($user->getFullName())->isNotEmpty()->isEqualTo('asd asd')
                ->string($user->getEmail())->isNotEmpty()->isEqualTo('test@buckhill.co.uk')
                ->string($user->getTimeZone())->isEqualTo('UTC')
                ->object($user->getTimeZone(true))->isInstanceOf(\DateTimeZone::class)
                ->string($user->getDateTimeFormat())->isEqualTo('DD.MM.YYYY')
                ->integer($user->getAuthstackID())->isEqualTo(10)
                ->object($user->getAttribute('sn'))->isInstanceOf(Entry::class)
                ->boolean($user->isAdmin())->isFalse()
        ;

    }

    public function testRetrieveProfileImage()
    {
        $this->given($user = $this->getUser())
            ->then
                ->object($user)->isInstanceOf(LDAPUser::class)

            ;
    }

    public function testRetrieveTimeZone()
    {
        $this->given($user = $this->getUser())
            ->then
            ->string($user->getTimeZone())->isEqualTo('UTC')
            ->object($user->getTimeZone(true))->isInstanceOf(\DateTimeZone::class)

        ;
    }

    public function testRetrieveDateTimeFormat()
    {
        $this->given($user = $this->getUser())
            ->then
            ->string($user->getDateTimeFormat())->isEqualTo('DD.MM.YYYY')

        ;
    }

    protected function getUser()
    {
        config()->set('LDAPTest', $this->getConfig());

        $driver = LDAPDriver::initialize('LDAPTest', 2);

        $driver->connect();

        $user = $driver->getUserByPrimaryKey('test@buckhill.co.uk');

        return $user;
    }


    protected function getConfig()
    {
        return [
            'dc' => 'dc=buckhill,dc=co,dc=uk',
            'ou' => 'ou=users',
            'uid' => 'uid',
            'attributes' => [
                'memberof', 'cn', 'dn', 'mail', 'uid', 'givenname', 'sn',
                'telephonenumber', 'postaladdress', 'o', 'l', 'postalcode', 'st',
            ],
            'administrative' => [
                // Which LDAP attribute to inspect
                'attribute' => 'memberof',

                // What value should the attribute contain
                'value' => 'cn=administrators,ou=groups,dc=buckhill,dc=co,dc=uk'
            ],
            'settings' => [
                // The key is used by User class and it maps to value
                // So, if we want to pull out someone's name, we need to change "givenname" to something else
                // since the 'name' will be used by the library to find out which LDAP attribute to pull out
                'name' => 'givenname',
                'lastname' => 'sn',
                'email' => 'mail'
            ],
            'connection' => [
                'host' => '10.0.0.188',
                'port' => 389,
                'base_dn' => 'dc=buckhill,dc=co,dc=uk',
                'TLS' => false,
                'options' => [
                    [
                        'name' => LDAP_OPT_PROTOCOL_VERSION,
                        'value' => 3,
                    ],
                ],
            ],
        ];
    }

    protected function getValidCredentials()
    {
        return [
            'username' => 'test@buckhill.co.uk',
            'password' => 'test',
        ];
    }
}