From 583ee314b44e95e5410ab3d58dc9569e14a09b64 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 20 Mar 2012 15:42:59 +0100 Subject: [PATCH] Refactor caption serialization --- lib/classes/caption/record.class.php | 96 ++++++++- lib/classes/record/exportElement.class.php | 2 +- lib/classes/task/period/ftp.class.php | 4 +- .../PhraseanetPHPUnitAbstract.class.inc | 2 +- lib/unitTest/caption/caption_recordTest.php | 182 ++++++++++++++++++ lib/unitTest/record/adapterTest.php | 13 -- 6 files changed, 280 insertions(+), 19 deletions(-) create mode 100644 lib/unitTest/caption/caption_recordTest.php diff --git a/lib/classes/caption/record.class.php b/lib/classes/caption/record.class.php index 49a5b65670..ff1ae42241 100644 --- a/lib/classes/caption/record.class.php +++ b/lib/classes/caption/record.class.php @@ -53,6 +53,97 @@ class caption_record implements caption_interface, cache_cacheableInterface return $this; } + const SERIALIZE_XML = 'xml'; + const SERIALIZE_YAML = 'yaml'; + + public function serialize($format) + { + switch ($format) + { + case self::SERIALIZE_XML: + return $this->serializeXML(); + break; + case self::SERIALIZE_YAML: + return $this->serializeYAML(); + break; + default: + throw new \Exception(sprintf('Unknown format %s', $format)); + break; + } + } + + protected function serializeYAML() + { + $buffer = array(); + + foreach ($this->get_fields() as $field) + { + $vi = $field->get_values(); + + if ($field->is_multi()) + { + $buffer[$field->get_name()] = array(); + foreach ($vi as $value) + { + $val = $value->getValue(); + $buffer[$field->get_name()][] = ctype_digit($val) ? (int) $val : $val; + } + } + else + { + $value = array_pop($vi); + $val = $value->getValue(); + $buffer[$field->get_name()] = ctype_digit($val) ? (int) $val : $val; + } + } + + $buffer = array('record' => array('description' => $buffer)); + + $dumper = new Symfony\Component\Yaml\Dumper(); + + return $dumper->dump($buffer, 3); + } + + protected function serializeXML() + { + $dom_doc = new DOMDocument('1.0', 'UTF-8'); + $dom_doc->formatOutput = true; + $dom_doc->standalone = true; + + $record = $dom_doc->createElement('record'); + $record->setAttribute('record_id', $this->record->get_record_id()); + $dom_doc->appendChild($record); + $description = $dom_doc->createElement('description'); + $record->appendChild($description); + + foreach ($this->get_fields() as $field) + { + $values = $field->get_values(); + + foreach ($values as $value) + { + $elem = $dom_doc->createElement($field->get_name()); + $elem->appendChild($dom_doc->createTextNode($value->getValue())); + $elem->setAttribute('meta_id', $value->getId()); + $elem->setAttribute('meta_struct_id', $field->get_meta_struct_id()); + $description->appendChild($elem); + } + } + + $doc = $dom_doc->createElement('doc'); + + $tc_datas = $this->record->get_technical_infos(); + + foreach ($tc_datas as $key => $data) + { + $doc->setAttribute($key, $data); + } + + $record->appendChild($doc); + + return $dom_doc->saveXML(); + } + protected function retrieve_fields() { if (is_array($this->fields)) @@ -83,7 +174,7 @@ class caption_record implements caption_interface, cache_cacheableInterface try { $databox_meta_struct = databox_field::get_instance($this->databox, $row['structure_id']); - $metadata = new caption_field($databox_meta_struct, $this->record); + $metadata = new caption_field($databox_meta_struct, $this->record); $rec_fields[$databox_meta_struct->get_id()] = $metadata; $dces_element = $metadata->get_databox_field()->get_dces_element(); @@ -133,7 +224,6 @@ class caption_record implements caption_interface, cache_cacheableInterface foreach ($this->retrieve_fields() as $meta_struct_id => $field) { if ($field->get_name() == $fieldname) - return $field; } @@ -206,7 +296,7 @@ class caption_record implements caption_interface, cache_cacheableInterface foreach ($fields as $key => $value) { - if(!isset($fields[$key])) + if (!isset($fields[$key])) continue; //if(strpos($fields[$key]['value'], 'get_xml(); + $xml = $this->get_caption()->serialize(caption_record::SERIALIZE_XML); if ($xml) { diff --git a/lib/classes/task/period/ftp.class.php b/lib/classes/task/period/ftp.class.php index a021fbc3d2..9d9aaaa351 100644 --- a/lib/classes/task/period/ftp.class.php +++ b/lib/classes/task/period/ftp.class.php @@ -470,7 +470,9 @@ class task_period_ftp extends task_appboxAbstract { $sbas_id = phrasea::sbasFromBas($base_id); $record = new record_adapter($sbas_id, $record_id); - $sdcaption = $record->get_xml(); + + $sdcaption = $record->get_caption()->serialize(caption_record::SERIALIZE_XML); + $remotefile = $file["filename"]; if ($subdef == 'caption') diff --git a/lib/unitTest/PhraseanetPHPUnitAbstract.class.inc b/lib/unitTest/PhraseanetPHPUnitAbstract.class.inc index b810c2d72e..92968cbfc0 100644 --- a/lib/unitTest/PhraseanetPHPUnitAbstract.class.inc +++ b/lib/unitTest/PhraseanetPHPUnitAbstract.class.inc @@ -1325,7 +1325,7 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase } if (self::$record_1 instanceof record_adapter) { - self::$record_1->delete(); +// self::$record_1->delete(); self::$record_1 = null; } if (self::$record_2 instanceof record_adapter) diff --git a/lib/unitTest/caption/caption_recordTest.php b/lib/unitTest/caption/caption_recordTest.php new file mode 100644 index 0000000000..5c76472447 --- /dev/null +++ b/lib/unitTest/caption/caption_recordTest.php @@ -0,0 +1,182 @@ +object = new caption_record(self::$record_1, self::$record_1->get_databox()); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testSerialize(). + */ + public function testSerializeXML() + { + + foreach (self::$record_1->get_databox()->get_meta_structure() as $databox_field) + { + $n = $databox_field->is_multi() ? 3 : 1; + + for ($i = 0; $i < $n; $i++) + { + \caption_Field_Value::create($databox_field, self::$record_1, \random::generatePassword()); + } + } + + $xml = $this->object->serialize(\caption_record::SERIALIZE_XML); + + $sxe = simplexml_load_string($xml); + $this->assertInstanceOf('SimpleXMLElement', $sxe); + + foreach (self::$record_1->get_caption()->get_fields() as $field) + { + if($field->get_databox_field()->is_multi()) + { + $tagname = $field->get_name(); + $retrieved = array(); + foreach($sxe->description->$tagname as $value) + { + $retrieved[] = (string) $value; + } + + $values = $field->get_values(); + $this->assertEquals(count($values), count($retrieved)); + foreach($values as $val) + { + $this->assertTrue(in_array($val->getValue(), $retrieved)); + } + } + else + { + $tagname = $field->get_name(); + $value = array_pop($field->get_values()); + $this->assertEquals($value->getValue(), (string) $sxe->description->$tagname); + } + } + } + + public function testSerializeYAML() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testGet_fields(). + */ + public function testGet_fields() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testGet_field(). + */ + public function testGet_field() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testGet_dc_field(). + */ + public function testGet_dc_field() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testGet_highlight_fields(). + */ + public function testGet_highlight_fields() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testGet_cache_key(). + */ + public function testGet_cache_key() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testGet_data_from_cache(). + */ + public function testGet_data_from_cache() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testSet_data_to_cache(). + */ + public function testSet_data_to_cache() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testDelete_data_from_cache(). + */ + public function testDelete_data_from_cache() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + +} + +?> diff --git a/lib/unitTest/record/adapterTest.php b/lib/unitTest/record/adapterTest.php index 40bbb4811e..8559b24539 100644 --- a/lib/unitTest/record/adapterTest.php +++ b/lib/unitTest/record/adapterTest.php @@ -246,19 +246,6 @@ class record_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract $this->assertTrue((static::$record_1->get_caption() instanceof caption_record)); } - public function testGet_xml() - { - $xml = self::$record_1->get_xml(); - $sxe = simplexml_load_string($xml); - $this->assertInstanceOf('SimpleXMLElement', $sxe); - - foreach (self::$record_1->get_caption()->get_fields() as $field) - { - $tagname = $field->get_name(); - $this->assertEquals($field->get_serialized_values(), (string) $sxe->description->$tagname); - } - } - public function testGet_original_name() { $this->assertTrue(static::$record_1->get_original_name() === self::$record_sf_1->getFilename());