dolibarr/test/phpunit/UserTest.php

317 lines
9.1 KiB
PHP
Raw Permalink Normal View History

2010-04-28 10:34:12 +02:00
<?php
2015-04-21 16:57:45 +02:00
/* Copyright (C) 2010-2015 Laurent Destailleur <eldy@users.sourceforge.net>
2010-04-28 10:34:12 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2010-04-28 10:34:12 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2011-12-17 21:58:44 +01:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* or see http://www.gnu.org/
2010-04-28 10:34:12 +02:00
*/
/**
2010-08-16 01:19:46 +02:00
* \file test/phpunit/UserTest.php
2015-01-06 17:54:36 +01:00
* \ingroup test
2010-10-03 20:53:40 +02:00
* \brief PHPUnit test
2015-01-06 17:54:36 +01:00
* \remarks To run this script as CLI: phpunit filename.php
2010-04-28 10:34:12 +02:00
*/
global $conf,$user,$langs,$db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
2010-08-16 01:19:46 +02:00
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/user/class/user.class.php';
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
if (empty($user->id)) {
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->getrights();
2010-04-28 10:34:12 +02:00
}
2010-07-15 01:10:56 +02:00
$conf->global->MAIN_DISABLE_ALL_MAILS=1;
2010-04-28 10:34:12 +02:00
/**
2011-09-23 14:21:00 +02:00
* Class for PHPUnit tests
*
2010-04-28 10:34:12 +02:00
* @backupGlobals disabled
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
class UserTest extends PHPUnit_Framework_TestCase
{
2015-01-06 17:54:36 +01:00
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @return UserTest
*/
function __construct()
{
//$this->sharedFixture
global $conf,$user,$langs,$db;
$this->savconf=$conf;
$this->savuser=$user;
$this->savlangs=$langs;
$this->savdb=$db;
print __METHOD__." db->type=".$db->type." user->id=".$user->id;
//print " - db ".$db->db;
print "\n";
}
// Static methods
public static function setUpBeforeClass()
2010-04-28 10:34:12 +02:00
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
print __METHOD__."\n";
2010-04-28 10:34:12 +02:00
}
2015-01-06 17:54:36 +01:00
// tear down after class
2010-04-28 10:34:12 +02:00
public static function tearDownAfterClass()
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$db->rollback();
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
print __METHOD__."\n";
2010-04-28 10:34:12 +02:00
}
2015-01-06 17:54:36 +01:00
/**
* Init phpunit tests
*
* @return void
*/
2010-04-28 10:34:12 +02:00
protected function setUp()
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
print __METHOD__."\n";
2010-04-28 10:34:12 +02:00
}
2012-02-15 13:41:05 +01:00
2015-01-06 17:54:36 +01:00
/**
* End phpunit tests
*
* @return void
*/
2010-04-28 10:34:12 +02:00
protected function tearDown()
{
2015-01-06 17:54:36 +01:00
print __METHOD__."\n";
2010-04-28 10:34:12 +02:00
}
/**
2012-02-17 16:02:35 +01:00
* testUserCreate
*
2015-01-06 17:54:36 +01:00
* @return void
2010-04-28 10:34:12 +02:00
*/
public function testUserCreate()
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new User($this->savdb);
$localobject->initAsSpecimen();
$result=$localobject->create($user);
$this->assertLessThan($result, 0);
print __METHOD__." result=".$result."\n";
return $result;
2010-04-28 10:34:12 +02:00
}
/**
2012-02-17 16:02:35 +01:00
* testUserFetch
*
2015-01-06 17:54:36 +01:00
* @param int $id Id of user
* @return void
* @depends testUserCreate
2010-04-28 10:34:12 +02:00
* The depends says test is run only if previous is ok
*/
public function testUserFetch($id)
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new User($this->savdb);
$result=$localobject->fetch($id);
$this->assertLessThan($result, 0);
print __METHOD__." id=".$id." result=".$result."\n";
return $localobject;
2010-04-28 10:34:12 +02:00
}
/**
2012-02-17 16:02:35 +01:00
* testUserUpdate
*
2015-01-06 17:54:36 +01:00
* @param Object $localobject User
* @return void
* @depends testUserFetch
2010-04-28 10:34:12 +02:00
* The depends says test is run only if previous is ok
*/
public function testUserUpdate($localobject)
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
$this->changeProperties($localobject);
$result=$localobject->update($user);
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
2013-04-01 14:49:33 +02:00
2015-01-06 17:54:36 +01:00
// Test everything are still same than specimen
$newlocalobject=new User($this->savdb);
$newlocalobject->initAsSpecimen();
$this->changeProperties($newlocalobject);
2015-04-21 16:57:45 +02:00
$this->assertEquals($this->objCompare($localobject,$newlocalobject,true,array('id','socid','societe_id','ref','pass_indatabase','pass_indatabase_crypted','datec','datem','datelastlogin','datepreviouslogin')), array()); // Actual, Expected
2013-04-01 14:49:33 +02:00
return $localobject;
2010-04-28 10:34:12 +02:00
}
/**
2012-02-17 16:02:35 +01:00
* testUserDisable
*
2015-01-06 17:54:36 +01:00
* @param Object $localobject User
* @return void
* @depends testUserUpdate
2010-04-28 10:34:12 +02:00
* The depends says test is run only if previous is ok
*/
public function testUserDisable($localobject)
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
$result=$localobject->setstatus(0);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
2010-04-28 10:34:12 +02:00
2015-01-06 17:54:36 +01:00
$this->assertLessThan($result, 0);
2013-04-01 14:49:33 +02:00
2015-01-06 17:54:36 +01:00
return $localobject;
2010-04-28 10:34:12 +02:00
}
2010-10-13 21:01:22 +02:00
/**
2012-02-17 16:02:35 +01:00
* testUserOther
*
2015-01-06 17:54:36 +01:00
* @param Object $localobject User
* @return void
2010-10-13 21:01:22 +02:00
* @depends testUserDisable
* The depends says test is run only if previous is ok
*/
public function testUserOther($localobject)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
/*$result=$localobject->setstatus(0);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
*/
$localobject->info($localobject->id);
print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
$this->assertNotEquals($localobject->date_creation, '');
return $localobject->id;
}
/**
2012-02-17 16:02:35 +01:00
* testUserDelete
*
2015-01-06 17:54:36 +01:00
* @param Object $id User
* @return void
* @depends testUserOther
2010-04-28 10:34:12 +02:00
* The depends says test is run only if previous is ok
*/
public function testUserDelete($id)
{
2015-01-06 17:54:36 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new User($this->savdb);
$result=$localobject->fetch($id);
$result=$localobject->delete($id);
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $result;
2010-04-28 10:34:12 +02:00
}
2013-04-01 14:49:33 +02:00
2013-06-05 16:12:07 +02:00
/**
* Edit an object to test updates
*
2015-01-06 17:54:36 +01:00
* @param mixed $localobject Object Facture
* @return void
2013-06-05 16:12:07 +02:00
*/
public function changeProperties(&$localobject)
{
2015-01-06 17:54:36 +01:00
$localobject->note='New note after update';
2013-06-05 16:12:07 +02:00
}
/**
* Compare all public properties values of 2 objects
*
2015-01-06 17:54:36 +01:00
* @param Object $oA Object operand 1
* @param Object $oB Object operand 2
* @param boolean $ignoretype False will not report diff if type of value differs
* @param array $fieldstoignorearray Array of fields to ignore in diff
* @return array Array with differences
2013-06-05 16:12:07 +02:00
*/
public function objCompare($oA,$oB,$ignoretype=true,$fieldstoignorearray=array('id'))
{
2015-01-06 17:54:36 +01:00
$retAr=array();
if (get_class($oA) !== get_class($oB)) {
$retAr[]="Supplied objects are not of same class.";
} else {
$oVarsA=get_object_vars($oA);
$oVarsB=get_object_vars($oB);
$aKeys=array_keys($oVarsA);
foreach($aKeys as $sKey) {
2015-04-21 16:57:45 +02:00
if (in_array($sKey,$fieldstoignorearray))
2015-01-06 17:54:36 +01:00
continue;
if (! $ignoretype && $oVarsA[$sKey] !== $oVarsB[$sKey]) {
$retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
}
if ($ignoretype && $oVarsA[$sKey] != $oVarsB[$sKey]) {
$retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
}
}
}
return $retAr;
2013-04-01 14:49:33 +02:00
}
2010-04-28 10:34:12 +02:00
}