Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_testcase_dcp.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Pu;
8 /**
9  * @author Anakeen
10  * @package Dcp\Pu
11  */
12 
13 $pubdir = ".";
14 set_include_path(get_include_path() . PATH_SEPARATOR . "$pubdir/DCPTEST:$pubdir/WHAT");
15 require_once 'WHAT/autoload.php';
16 include_once ("FDL/Class.Doc.php"); // to include some libraries
17 class TestCaseDcp extends \PHPUnit_Framework_TestCase
18 {
19  /**
20  * DbAccess string
21  *
22  * @var string
23  */
24  protected static $dbaccess;
25  /**
26  * Dbobj use for transaction
27  *
28  * @var \DbObj
29  */
30  protected static $odb;
31  /**
32  * User keep in cache during the sudo
33  *
34  * @var string
35  */
36  protected static $user = null;
37  /**
38  * Store original include_path
39  */
40  protected static $include_path = null;
41 
42  protected static $testDirectory = "DCPTEST";
43 
44  protected static $importCsvEnclosure = "auto";
45  protected static $importCsvSeparator = "auto";
46 
47  protected function setUp()
48  {
49  $this->log(sprintf("========== %s ========", $this->getName()));
50  $this->connectUser("admin");
51  $this->beginTransaction();
52  }
53  protected function tearDown()
54  {
55  $this->rollbackTransaction();
56  }
57 
58  public static function setUpBeforeClass()
59  {
60  }
61 
62  public static function log($text)
63  {
64  file_put_contents(TestSuiteDcp::logFile, sprintf("[%s] %s\n", date("Y-m-d H:i:s") , $text) , FILE_APPEND);
65  }
66  /**
67  * Make a begin in the db
68  *
69  * @return void
70  */
71  protected static function beginTransaction()
72  {
74  if (!self::$odb) {
75  self::$odb = new \DbObj(self::$dbaccess);
76  }
77  self::$odb->savePoint('putransaction');
78  // $err = simpleQuery(self::$dbaccess, "begin", $r);
79 
80  }
81  /**
82  * Make a rollback in the db
83  *
84  * @return void
85  */
86  protected static function rollbackTransaction()
87  {
88 
89  self::$odb->rollbackPoint('putransaction');
90  //$err = simpleQuery(self::$dbaccess, "rollback", $r);
91 
92  }
93  /**
94  * Connect as a user
95  *
96  * @param string $login login of the user
97  *
98  * @return void
99  */
100  protected static function connectUser($login = "admin")
101  {
102  global $action;
103  if (!$action) {
106  }
107  }
108  /**
109  * Current action
110  * @return \Action
111  */
112  protected static function &getAction()
113  {
114  global $action;
115  if (!$action) {
116  self::connectUser();
117  }
118 
119  if (!$action->dbid) {
120  if (!$action->dbid) $action->init_dbid();
121  if (!$action->dbid) error_log(__METHOD__ . "lost action dbid");
122  $action->init_dbid();
123  }
124  return $action;
125  }
126  /**
127  * Current application
128  * @return \Application
129  */
130  protected static function getApplication()
131  {
132  global $action;
133  if ($action) return $action->parent;
134  return null;
135  }
136  /**
137  * return a single value from DB
138  *
139  * @param string $sql a query with a single fields in from part
140  *
141  * @return string
142  */
143  protected function _DBGetValue($sql)
144  {
145  $err = simpleQuery(self::$dbaccess, $sql, $sval, true, true);
146  $this->assertEquals("", $err, sprintf("database select error", $sql));
147  return $sval;
148  }
149  /**
150  * reset shared documents
151  *
152  * @return void
153  */
154  protected static function resetDocumentCache()
155  {
157  }
158  /**
159  * use another user
160  *
161  * @param string $login
162  * @return \Account
163  * @throws \Dcp\Exception
164  */
165  protected static function sudo($login)
166  {
167  $u = new \Account(self::$dbaccess);
168  if (!$u->setLoginName($login)) {
169  throw new \Dcp\Exception("login $login not exist");
170  }
171 
172  global $action;
173  self::$user = $action->user;
174  $action->user = $u;
175  self::resetDocumentCache();
176  return $u;
177  }
178  /**
179  * exit sudo
180  *
181  * @return void
182  */
183  protected static function exitSudo()
184  {
185  global $action;
186  if (self::$user) {
187  $action->user = self::$user;
188  self::$user = null;
189  }
190  }
191  /**
192  * Import a file document description
193  *
194  * @param string $file file path
195  * @return array
196  * @throws \Dcp\Exception
197  */
198  protected static function importDocument($file)
199  {
200  if (is_array($file)) {
201  return self::importDocuments($file);
202  }
203 
204  $realfile = $file;
205  if (!file_exists($realfile)) {
206  $ext = substr($file, strrpos($file, '.') + 1);
207  if ($ext == "ods" || $ext == "csv") {
208  $realfile = static::$testDirectory . "/" . $file;
209  } else {
210  $realfile = static::$testDirectory . "/Layout/" . $file;
211  }
212  }
213  if (!file_exists($realfile)) {
214  throw new \Dcp\Exception(sprintf("File '%s' not found in '%s'.", $file, $realfile));
215  }
216  $oImport = new \ImportDocument();
217  $oImport->setCsvOptions(static::$importCsvSeparator, static::$importCsvEnclosure);
218  //error_log(__METHOD__."import $realfile");
219  $oImport->setVerifyAttributeAccess(false);
220  $cr = $oImport->importDocuments(self::getAction() , $realfile);
221  $err = $oImport->getErrorMessage();
222  if ($err) throw new \Dcp\Exception($err);
223  return $cr;
224  }
225  /**
226  * Import multiple files specified as a array list
227  * @param array $fileList list of files to import
228  * @return array
229  */
230  protected static function importDocuments($fileList)
231  {
232  $cr = array();
233  if (!is_array($fileList)) {
234  $cr[] = self::importDocument($fileList);
235  return $cr;
236  }
237 
238  foreach ($fileList as $file) {
239  $cr[] = self::importDocument($file);
240  }
241  return $cr;
242  }
243  /**
244  * Import CSV data
245  * @param string $data CSV data
246  * @throws \Dcp\Exception
247  */
248  public function importCsvData($data)
249  {
250  $tmpFile = tempnam(getTmpDir() , "importData");
251  if ($tmpFile === false) {
252  throw new \Dcp\Exception(sprintf("Error creating temporary file in '%s'.", getTmpDir()));
253  }
254  $ret = rename($tmpFile, $tmpFile . '.csv');
255  if ($ret === false) {
256  throw new \Dcp\Exception(sprintf("Error renaming '%s' to '%s'.", $tmpFile, $tmpFile . '.csv'));
257  }
258  $tmpFile = $tmpFile . '.csv';
259  $ret = file_put_contents($tmpFile, $data);
260  if ($ret === false) {
261  throw new \Dcp\Exception(sprintf("Error writing to file '%s'.", $tmpFile));
262  }
263  $this->importDocument($tmpFile);
264  unlink($tmpFile);
265  }
266  /**
267  * Set the include_path INI parameter
268  * @param string $include_path the new include_path to use
269  */
270  public static function setIncludePath($include_path)
271  {
272  if (self::$include_path == null) {
273  self::$include_path = ini_get('include_path');
274  }
275  ini_set('include_path', $include_path);
276  }
277  /**
278  * Set back the original include_path INI parameter
279  */
280  public static function resetIncludePath()
281  {
282  if (self::$include_path !== null) {
283  ini_set('include_path', self::$include_path);
284  }
285  }
286  /**
287  * Mark test as incomplete (skip) if a core param is not equal
288  * to the required value.
289  *
290  * @param string $paramName the core parameter name
291  * @param string $requiredValue the required value
292  * @param bool $markTestIncomplete automatically calls the markTestIncomplete() method if the value is different
293  * @return bool
294  */
295  public function requiresCoreParamEquals($paramName, $requiredValue, $markTestIncomplete = true)
296  {
297  $value = getParam($paramName, '');
298  if ($value === $requiredValue) {
299  return true;
300  }
301  if ($markTestIncomplete === true) {
302  $this->markTestIncomplete(sprintf("Test requires %s '%s' (found '%s').", $paramName, $requiredValue, $value));
303  }
304  return false;
305  }
306 }
307 
309 {
310  private $value = null;
311  public function __construct($value)
312  {
313  $this->value = $value;
314  return $this;
315  }
316  public function __toString()
317  {
318  return $this->resolve($this->value);
319  }
320  private function resolve($value)
321  {
322  $id = getIdFromName('', $value);
323  if (is_numeric($id)) {
324  return (string)$id;
325  }
326  return (string)$value;
327  }
328 }
static sudo($login)
static connectUser($login="admin")
global $action
static importDocuments($fileList)
$ret
$file
static importDocument($file)
if($path[0]== '/') $realfile
Definition: resizeimg.php:176
static setIncludePath($include_path)
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
$login
Definition: dav.php:40
WhatInitialisation($session=null)
Definition: Lib.Common.php:767
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
getDbAccess()
Definition: Lib.Common.php:368
setSystemLogin($login)
Definition: Lib.Common.php:786
$dbaccess
Definition: checkVault.php:17
getIdFromName($dbaccess, $name)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
static rollbackTransaction()
if($dirid) $oImport
$value
requiresCoreParamEquals($paramName, $requiredValue, $markTestIncomplete=true)
$data
← centre documentaire © anakeen