<?php

namespace Alchemy\Tests\Phrasea\Vocabulary\ControlProvider;

use Alchemy\Phrasea\Vocabulary\ControlProvider\UserProvider;

/**
 * @group functional
 * @group legacy
 */
class UserProviderTest extends \PhraseanetTestCase
{
    /**
     * @var UserProvider
     */
    protected $object;

    public function setUp()
    {
        parent::setUp();
        $this->object = new UserProvider(self::$DI['app']);
    }

    /**
     * Verify that Type is scalar and that the classname is like {Type}Provider
     */
    public function testGetType()
    {
        $type = $this->object->getType();

        $this->assertTrue(is_scalar($type));

        $data = explode('\\', get_class($this->object));
        $classname = array_pop($data);

        $this->assertEquals($classname, $type . 'Provider');
    }

    public function testGetName()
    {
        $this->assertTrue(is_scalar($this->object->getName()));
    }

    public function testFind()
    {
        // mandatory until user rights are managed by doctrine
        //self::$DI['app']['orm.em'] = EntityManager::create(self::$DI['app']['conf']->get(['main', 'database']), self::$DI['app']['db.config'], self::$DI['app']['db.event_manager']);

        $app = $this->getApplication();
        $params = $app['db.appbox.info'];
        $info = $app['db.info']($params);
        $key = $app['orm.add']($info);

        self::$DI['app']['orm.em'] = $app['orm.ems'][$key];

        $user = self::$DI['app']['manipulator.user']->createUser(uniqid('test'), 'a_password', uniqid('test').'@domain.fr');
        self::giveRightsToUser(self::$DI['app'], $user);
        $user->setFirstName('John');
        $user->setLastName('Doe');
        self::$DI['app']['orm.em']->persist($user);
        self::$DI['app']['orm.em']->flush();

        $results = $this->object->find('BABE', $user,  self::$DI['collection']->get_databox());

        $this->assertInternalType('array', $results);

        $results = $this->object->find($user->getEmail(), $user, self::$DI['collection']->get_databox());

        $this->assertInternalType('array', $results);
        $this->assertGreaterThan(0, count($results), 'There should be more users matching');

        $results = $this->object->find($user->getFirstName(), $user,  self::$DI['collection']->get_databox());

        $this->assertInternalType('array', $results);
        $this->assertGreaterThan(0, count($results), 'There should be more users matching');

        $results = $this->object->find($user->getLastName(), $user,  self::$DI['collection']->get_databox());

        $this->assertInternalType('array', $results);
        $this->assertGreaterThan(0, count($results), 'There should be more users matching');
        self::$DI['app']['manipulator.user']->delete($user);
    }

    public function testValidate()
    {
        $this->assertFalse($this->object->validate(-200));
        $this->assertFalse($this->object->validate('A'));
        $this->assertTrue($this->object->validate(self::$DI['user']->getId()));
    }

    public function testGetValue()
    {
        try {
            $this->object->getValue(-200);
            $this->fail('Should raise an exception');
        } catch (\Exception $e) {

        }

        try {
            $this->object->getValue('A');
            $this->fail('Should raise an exception');
        } catch (\Exception $e) {

        }

        $this->assertEquals(self::$DI['user']->getDisplayName(), $this->object->getValue(self::$DI['user']->getId()));
    }
}
