<?php

class UserTest extends PHPUnit_Framework_TestCase
{
	public function testUserPassword()
	{
		list($server, $mysqlConfig) = require("test/config.php");
		$dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
		$dbh->exec("CREATE DATABASE ".$mysqlConfig[4][1]);
		\Entity\ModelBase::init($mysqlConfig);
		$context = new \Tools\Context($server, false);
		\Entity\ModelBase::setup();

		$user = new \Entity\User();
		$user->setPassword("test");
		$this->assertTrue($user->checkPassword("test"));
		$this->assertFalse($user->checkPassword("test2"));
		$this->assertEquals(60, strlen($user->password));
		$this->assertNotEquals("test", $user->password);
		$user->password = "test2";
		$this->assertFalse($user->checkPassword("test"));
		$this->assertTrue($user->checkPassword("test2"));
		$this->assertEquals(60, strlen($user->password));
		$this->assertNotEquals("test2", $user->password);
	}

	public function testSave()
	{
		list($server, $mysqlConfig) = require("test/config.php");
		$dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
		$dbh->exec("DROP DATABASE ".$mysqlConfig[4][1]);
		$dbh->exec("CREATE DATABASE ".$mysqlConfig[4][1]);
		\Entity\ModelBase::init($mysqlConfig);
		$context = new \Tools\Context($server, false);
		\Entity\ModelBase::setup();

		$user = new \Entity\User();
		$today = new \DateTime();
		$today = $today->format("Y-m-d");
		$this->assertEquals($today, $user->register);
		$this->assertEquals($today, $user->lastVisit);
		$this->assertEquals($today, $user->lastConnect);
		$this->assertEquals($server["REMOTE_ADDR"], $user->registerIp);
		$this->assertEquals($server["REMOTE_ADDR"], $user->lastVisitIp);
		$this->assertEquals($server["REMOTE_ADDR"], $user->lastConnectIp);
		$user->setPassword("test");
		$user->email = "testUser@testhostname.dev";
		$user->sexe = "MALE";
		$user->optin = true;
		$this->assertNull($user->id);
		$user->save();
		$this->assertNotNull($user->id);
		$this->assertInternalType('int', $user->id);
		$this->assertEquals(1, $user->id);
		$user = new \Entity\User();
		$user->setPassword("test");
		$user->email = "testUser2@testhostname.dev";
		$user->sexe = "FEMALE";
		$user->optin = false;
		$this->assertNull($user->id);
		$user->save();
		$this->assertNotNull($user->id);
		$this->assertInternalType('int', $user->id);
		$this->assertEquals(2, $user->id);

		$user1 = new \Entity\User(1);
		$user2 = new \Entity\User(2);

		$today = new \DateTime();
		$today->setTime(0, 0, 0);
		$today = $today->format("Y-m-d H:i:s");
		$this->assertEquals($today, $user1->register);
		$this->assertEquals($today, $user1->lastVisit);
		$this->assertEquals($today, $user1->lastConnect);
		$this->assertEquals($server["REMOTE_ADDR"], $user1->registerIp);
		$this->assertEquals($server["REMOTE_ADDR"], $user1->lastVisitIp);
		$this->assertEquals($server["REMOTE_ADDR"], $user1->lastConnectIp);
		$this->assertEquals("testUser@testhostname.dev", $user1->email);
		$this->assertEquals("testUser2@testhostname.dev", $user2->email);
		$this->assertEquals("MALE", $user1->sexe);
		$this->assertEquals("FEMALE", $user2->sexe);
		$this->assertTrue($user1->optin == 1);
		$this->assertFalse($user2->optin == 1);
		$this->assertNotNull($user1->id);
		$this->assertInternalType('int', $user1->id);
		$this->assertEquals(1, $user1->id);
		$this->assertNotNull($user2->id);
		$this->assertInternalType('int', $user2->id);
		$this->assertEquals(2, $user2->id);

		$user1->sexe = "FEMALE";
		$user1->save();
		$user3 = new \Entity\User(1);
		$this->assertEquals("testUser@testhostname.dev", $user3->email);
		$this->assertEquals("FEMALE", $user3->sexe);
		$this->assertTrue($user3->optin == 1);
		$this->assertNotNull($user3->id);
		$this->assertInternalType('int', $user3->id);
		$this->assertEquals(1, $user3->id);
	}
}

