View source
<?php
class XMLExportViewsDataExportExporterTests extends ViewsDataExportExporterBaseTest {
protected $profile = 'testing';
public static function getInfo() {
return array(
'name' => 'XML Exporter Test',
'description' => 'Various tests for export using the XML exporter class.',
'group' => 'Views Data Export',
);
}
protected function getExporter($options = array()) {
require_once dirname(__FILE__) . '/../../exporters/views_data_export_exporter_xml.inc';
$classname = $this
->getExporterClassName();
return new $classname($options);
}
protected function getExporterClassName() {
return 'ViewsDataExportExporterXML';
}
protected function testBOF() {
$exporter = $this
->getExporter(array(
'field_labels' => array(
'name',
'age',
'job',
'created',
),
));
$BOF = $this
->executeBOF($exporter);
$expect = '<?xml version="1.0" encoding="UTF-8" ?>
<root>';
$result = strpos($BOF, $expect) == 0;
$this
->logVerboseResult($BOF, 'Actual result');
$this
->logVerboseResult($expect, 'Expected to contain');
$this
->assertTrue($result, 'The BOF is as expected.');
}
protected function testBodyWrite() {
$dataSet = array(
array(
'name' => 'John Lennon',
'their age' => 25,
'pets' => array(
'cat' => 'figaro',
'dog' => 'pluto',
),
'colours' => array(
'red',
'blue',
),
'their job' => 'they are a Singer',
'created' => gmmktime(0, 0, 0, 1, 1, 2000),
),
);
$result = $this
->executeFullWrite($this
->getExporter(), $dataSet, 0, array(
'name' => 'name',
'their age' => 'their age',
'pets' => 'pets',
'cat' => 'cat',
'dog' => 'dog',
'colours' => 'colours',
'their job' => 'their job',
'created' => 'created',
));
$expected = ' <item>
<name>John Lennon</name>
<their-age>25</their-age>
<pets>
<cat>figaro</cat>
<dog>pluto</dog>
</pets>
<colours>
<data>red</data>
<data>blue</data>
</colours>
<their-job>they are a Singer</their-job>
<created>946684800</created>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected result');
$this
->assertEqual($result, $expected, 'The content is as expected.');
}
protected function testTransform() {
$spaced_dataset = array(
array(
'name of artist' => 'John Lennon',
'their age' => 25,
'their job' => 'they are a Singer',
'created' => gmmktime(0, 0, 0, 1, 1, 2000),
),
);
$result = $this
->executeFullWrite($this
->getExporter(array(
'transform' => true,
'transform_type' => 'dash',
)), $spaced_dataset, 0, array(
'name of artist' => 'name of artist',
'their age' => 'their age',
'their job' => 'their job',
'created' => 'created',
));
$expected = ' <item>
<name-of-artist>John Lennon</name-of-artist>
<their-age>25</their-age>
<their-job>they are a Singer</their-job>
<created>946684800</created>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected result');
$this
->assertEqual($result, $expected, 'The content is as expected.');
$result = $this
->executeFullWrite($this
->getExporter(array(
'transform' => true,
'transform_type' => 'underline',
)), $spaced_dataset, 0, array(
'name of artist' => 'name of artist',
'their age' => 'their age',
'their job' => 'their job',
'created' => 'created',
));
$expected = ' <item>
<name_of_artist>John Lennon</name_of_artist>
<their_age>25</their_age>
<their_job>they are a Singer</their_job>
<created>946684800</created>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected result');
$this
->assertEqual($result, $expected, 'The content is as expected.');
$result = $this
->executeFullWrite($this
->getExporter(array(
'transform' => true,
'transform_type' => 'camelCase',
)), $spaced_dataset, 0, array(
'name of artist' => 'name of artist',
'their age' => 'their age',
'their job' => 'their job',
'created' => 'created',
));
$expected = ' <item>
<nameOfArtist>John Lennon</nameOfArtist>
<theirAge>25</theirAge>
<theirJob>they are a Singer</theirJob>
<created>946684800</created>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected result');
$this
->assertEqual($result, $expected, 'The content is as expected.');
$result = $this
->executeFullWrite($this
->getExporter(array(
'transform' => true,
'transform_type' => 'PascalCase',
)), $spaced_dataset, 0, array(
'name of artist' => 'name of artist',
'their age' => 'their age',
'their job' => 'their job',
'created' => 'created',
));
$expected = ' <item>
<NameOfArtist>John Lennon</NameOfArtist>
<TheirAge>25</TheirAge>
<TheirJob>they are a Singer</TheirJob>
<Created>946684800</Created>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected result');
$this
->assertEqual($result, $expected, 'The content is as expected.');
}
protected function testRootNode() {
$exporter = $this
->getExporter(array(
'field_labels' => array(
'name',
'age',
'job',
'created',
),
'root_node' => 'test_document_root',
));
$BOF = $this
->executeBOF($exporter);
$expected = '<test_document_root>';
$result = strpos($BOF, $expected) > -1;
$this
->logVerboseResult($BOF, 'Actual result');
$this
->logVerboseResult($expected, 'Expected to contain');
$this
->assertTrue($result, 'The Root Node is as expected.');
}
protected function testEntityEncode() {
$test_entity_encode_dataset = array(
array(
'name' => '<strong>John</strong>',
'age' => 25,
'job' => '<em>Singer</em>',
),
);
$result = $this
->executeFullWrite($this
->getExporter(array(
'no_entity_encode' => array(
'name' => 'name',
),
)), $test_entity_encode_dataset, 0, array(
'name' => 'name',
'age' => 'age',
'job' => 'job',
));
$expected = ' <item>
<name><strong>John</strong></name>
<age>25</age>
<job>&lt;em&gt;Singer&lt;/em&gt;</job>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected to contain');
$this
->assertEqual($result, $expected, 'The entities were encoded correctly.');
}
protected function testCData() {
$CDATA_dataset = array(
array(
'illegal_content' => 'illegal XML characters < & >',
'name' => 'John',
'age' => 25,
'job' => 'singer',
'created' => gmmktime(0, 0, 0, 1, 1, 2000),
),
);
$result = $this
->executeFullWrite($this
->getExporter(array(
'cdata_wrapper' => array(
'illegal_content',
),
)), $CDATA_dataset, 0, array(
'illegal_content' => 'illegal_content',
'name' => 'name',
'age' => 'age',
'job' => 'job',
'created' => 'created',
));
$expected = ' <item>
<illegal_content><![CDATA[illegal XML characters < & >]]></illegal_content>
<name>John</name>
<age>25</age>
<job>singer</job>
<created>946684800</created>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected result');
$this
->assertEqual($result, $expected, 'The content is as expected.');
}
protected function testEOF() {
$exporter = $this
->getExporter(array(
'field_labels' => array(
'name',
'age',
'job',
'created',
),
'root_node' => 'test_root_node',
));
$result = $this
->executeEOF($exporter);
$expect = '</test_root_node>' . PHP_EOL;
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expect, 'Expected to contain');
$this
->assertEqual($result, $expect, 'The EOF is as expected.');
}
protected function testTagValidity() {
$spaced_dataset = array(
array(
'name of artist' => 'John Lennon',
'their age' => 25,
'their job' => 'they are a Singer',
'created' => gmmktime(0, 0, 0, 1, 1, 2000),
),
);
$result = $this
->executeFullWrite($this
->getExporter(array(
'transform' => true,
'transform_type' => 'dash',
)), $spaced_dataset, 0, array(
'name of artist' => '',
'their age' => '1',
'their job' => 'BadCharacters>&<',
));
$expected = ' <item>
<no-name>John Lennon</no-name>
<data>25</data>
<BadCharacters>they are a Singer</BadCharacters>
<created>946684800</created>
</item>
';
$this
->logVerboseResult($result, 'Actual result');
$this
->logVerboseResult($expected, 'Expected result');
$this
->assertEqual($result, $expected, 'The content is as expected.');
}
}