Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_splitxmldocument.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 require_once 'PU_testcase_dcp.php';
14 
15 include_once 'FREEDOM/freedom_import_xml.php';
16 /**
17  * Test class for splitXmlDocument() function.
18  */
19 
21 {
22  static private $runid = 0;
23  static private $workDir = false;
24  public $errmsg = '';
25 
26  public static function setUpBeforeClass()
27  {
28  parent::setUpBeforeClass();
29  self::createWorkDir();
30  }
31 
32  public static function tearDownAfterClass()
33  {
34  parent::tearDownAfterClass();
35  $stat = stat(self::$workDir);
36  if ($stat['nlink'] <= 2) {
37  rmdir(self::$workDir);
38  }
39  }
40  /**
41  * @dataProvider dataSplitXmlDocument
42  */
44  {
45  self::$runid++;
46 
47  $testDir = self::$workDir . DIRECTORY_SEPARATOR . self::$runid;
48  mkdir($testDir);
49 
50  $src = 'DCPTEST' . DIRECTORY_SEPARATOR . 'Layout' . DIRECTORY_SEPARATOR . $data['xml'];
51  $workingXML = $testDir . DIRECTORY_SEPARATOR . $data['xml'];
52  $ret = copy($src, $workingXML);
53  if ($ret === false) {
54  throw new \Exception(sprintf("Could not copy '%s' to '%s'.", $src, $workingXML));
55  }
56  if (isset($data['xml_alter'])) {
57  $args = array();
58  if (isset($data['xml_alter_args'])) {
59  $args = $data['xml_alter_args'];
60  }
61  $ret = call_user_func(array(
62  $this,
63  $data['xml_alter']
64  ) , $workingXML, $args);
65  if ($ret === false) {
66  throw new \Exception($this->errmsg);
67  }
68  }
69  /* check splitXmlDocument() */
70  if (isset($data['expect_error']) && $data['expect_error'] === true) {
71  try {
72  \Dcp\Core\importXml::splitXmlDocument($workingXML, $testDir);
73  $this->assertTrue(false, "XML Error not detected");
74  }
75  catch(\Exception $e) {
76  $this->assertNotEmpty($e->getMessage() , sprintf("splitXmlDocument did not returned with an expected error"));
77  }
78 
79  return;
80  } else {
81  \Dcp\Core\importXml::splitXmlDocument($workingXML, $testDir);
82  }
83 
84  if (!isset($data['produces'])) {
85  return;
86  }
87  /* check that the expected files are present */
88  foreach ($data['produces'] as $file) {
89  $file = $testDir . DIRECTORY_SEPARATOR . $file;
90  $this->assertTrue(is_file($file) , sprintf("Required file '%s' has not been produced by splitXmlDocument.", $file));
91  }
92  /* check they are valid XML files */
93  foreach ($data['produces'] as $file) {
94  $file = $testDir . DIRECTORY_SEPARATOR . $file;
95  $this->assertTrue($this->isValidXML($file) , sprintf("Output file '%s' does not seems to be a valid XML file according to xmllint.", $file));
96  }
97 
98  $this->rm_Rf($testDir);
99  }
100  private static function createWorkDir()
101  {
102  $tmpdir = getTmpDir();
103  if (!is_dir($tmpdir)) {
104  throw new \Exception(sprintf("Invalid directory '%s'.", $tmpdir));
105  }
106  $tmpname = tempnam($tmpdir, 'PU_TEST_DCP_SPLITXMLDOCUMENT');
107  if ($tmpname === false) {
108  throw new \Exception(sprintf("Could not create temporary file in '%s'.", $tmpdir));
109  }
110  unlink($tmpname);
111  if (mkdir($tmpname, 0700) === false) {
112  throw new \Exception(sprintf("Could not create directory '%s'.", $tmpname));
113  }
114  self::$workDir = $tmpname;
115  }
116 
117  private function isValidXML($file)
118  {
119  $cmd = sprintf("xmllint --sax %s | grep -c '^SAX\\.error' > /dev/null 2>&1", escapeshellarg($file));
120  $ret = 0;
121  $out = system($cmd, $ret);
122  if ($ret == 0) {
123  /* If grep exit code is 0, it means it found a "SAX.error" line,
124  * which means there are errors in the XML file
125  */
126  return false;
127  }
128  /* grep found no "SAX.error" lines */
129  return true;
130  }
131  /** @noinspection PhpUnusedPrivateMethodInspection */
132  private function addBigNode($xml, $args = array())
133  {
134  $addNodeData = file_get_contents('DCPTEST' . DIRECTORY_SEPARATOR . 'Layout' . DIRECTORY_SEPARATOR . 'PU_data_dcp_splitxmldocument_bignode_template.xml');
135  if ($addNodeData === false) {
136  $this->errmsg = sprintf("Could not get content from XML file '%s'.", "PU_data_dcp_splitxmldocument_bignode_template.xml");
137  return false;
138  }
139  $xmlData = file_get_contents($xml);
140  if ($xmlData === false) {
141  $this->errmsg = sprintf("Could not get content from XML file '%s'.", $xml);
142  return false;
143  }
144  /*
145  * expand @VARIABLES@ (1st pass)
146  */
147  $addNodeElmts = preg_split('/(@[a-zA-Z_][a-zA-Z0-9_]+@)/', $addNodeData, -1, PREG_SPLIT_DELIM_CAPTURE);
148  foreach ($addNodeElmts as & $el) {
149  $m = array();
150  if (!preg_match('/^@(?<var>[a-zA-Z_][a-zA-Z0-9_]+)@$/', $el, $m)) {
151  continue;
152  }
153  if ($m['var'] == 'DATA') {
154  /* @DATA@ will be expanded in 2nd and final pass */
155  continue;
156  }
157  if (!isset($args[$m['var']])) {
158  continue;
159  }
160  $el = $args[$m['var']];
161  }
162  unset($el);
163  /*
164  * parse main XML document
165  */
166  $xmlElmts = array();
167  if (!preg_match(':^(?<top>.*)(?<bottom></documents>\s*)$:ms', $xmlData, $xmlElmts)) {
168  $this->errmsg = sprintf("Could not match XML document in XML file '%s'.", $xml);
169  return false;
170  }
171  $fh = fopen($xml, 'w');
172  if ($fh === false) {
173  $this->errmsg = sprintf("Could not open XML file '%s' for writing.", $xml);
174  return false;
175  }
176  /* write back the top part */
177  $ret = fwrite($fh, $xmlElmts['top']);
178  /*
179  * expand @DATA@ (2nd pass)
180  */
181  foreach ($addNodeElmts as & $el) {
182  $m = array();
183  if (!preg_match('/^@DATA@$/', $el, $m)) {
184  fwrite($fh, $el);
185  continue;
186  }
187  /* Generate BASE64 data */
188  $size = isset($args['size_in_MB']) ? $args['size_in_MB'] : 1;
189  $size = $size * 1024 * 1024;
190  $blockCount = floor($size / (3 * 1024));
191  $oneBlock = str_repeat("QUFB", 1024);
192  for ($i = 1; $i <= $blockCount; $i++) {
193  fwrite($fh, $oneBlock);
194  }
195  $remBytes = $size - $blockCount * 3 * 1024;
196  if ($remBytes == 1024) {
197  fwrite($fh, str_repeat("QUFB", 341) . "QQ==");
198  } elseif ($remBytes == 2048) {
199  fwrite($fh, str_repeat("QUFB", 682) . "QUE=");
200  }
201  }
202  unset($el);
203  /* write back the bottom part */
204  $ret = fwrite($fh, $xmlElmts['bottom']);
205  fclose($fh);
206  return $xml;
207  }
208 
209  private function rm_Rf($dir)
210  {
211  $type = filetype($dir);
212  if ($type != 'dir') {
213  unlink($dir);
214  return true;
215  }
216  $fh = opendir($dir);
217  if ($fh === false) {
218  return false;
219  }
220  while (($file = readdir($fh)) !== false) {
221  if ($file == '.' || $file == '..') {
222  continue;
223  }
224  $file = $dir . DIRECTORY_SEPARATOR . $file;
225  $this->rm_Rf($file);
226  }
227  closedir($fh);
228  rmdir($dir);
229  return true;
230  }
231 
232  public function dataSplitXmlDocument()
233  {
234  return array(
235  array(
236  array(
237  'description' => 'Small XML file',
238  'xml' => 'PU_data_dcp_splitxmldocument.xml',
239  'produces' => array(
240  '00000PU_DATA_DCP_SPLITXMLDOCUMENT_1.xml',
241  '00001PU_DATA_DCP_SPLITXMLDOCUMENT_2.xml',
242  '00002PU_DATA_DCP_SPLITXMLDOCUMENT_3.xml'
243  )
244  )
245  ) ,
246  array(
247  array(
248  'description' => 'Big XML file',
249  'xml' => 'PU_data_dcp_splitxmldocument.xml',
250  'xml_alter' => 'addBigNode',
251  'xml_alter_args' => array(
252  'NAME' => 'PU_DATA_DCP_SPLITXMLDOCUMENT_BIGNODE',
253  'TITLE' => 'big.bin',
254  'size_in_MB' => '100'
255  ) ,
256  'produces' => array(
257  '00000PU_DATA_DCP_SPLITXMLDOCUMENT_1.xml',
258  '00001PU_DATA_DCP_SPLITXMLDOCUMENT_2.xml',
259  '00002PU_DATA_DCP_SPLITXMLDOCUMENT_3.xml',
260  '00003PU_DATA_DCP_SPLITXMLDOCUMENT_BIGNODE.xml'
261  )
262  )
263  ) ,
264  array(
265  array(
266  'description' => 'Invalid root node',
267  'xml' => 'PU_data_dcp_splitxmldocument_invalid_root_node.xml',
268  'expect_error' => true
269  )
270  )
271  );
272  }
273 }
274 ?>
if(substr($wsh, 0, 1)!= '/') $args
$size
Definition: resizeimg.php:110
$ret
$file
foreach($argv as $arg) $cmd
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
$dir
Definition: resizeimg.php:144
static splitXmlDocument($xmlfiles, $splitdir)
$data
← centre documentaire © anakeen