JsonFFSTest.php
Go to the documentation of this file.00001 <?php
00014 class JsonFFSTest extends MediaWikiTestCase {
00015
00016 public function setUp() {
00017 parent::setUp();
00018 $this->groupConfiguration = array(
00019 'BASIC' => array(
00020 'class' => 'FileBasedMessageGroup',
00021 'id' => 'test-id',
00022 'label' => 'Test Label',
00023 'namespace' => 'NS_MEDIAWIKI',
00024 'description' => 'Test description',
00025 ),
00026 'FILES' => array(
00027 'class' => 'JsonFFS',
00028 'sourcePattern' => __DIR__ . '/../data/jsontest_%CODE%.json',
00029 'targetPattern' => 'jsontest_%CODE%.json',
00030 ),
00031 );
00032 }
00033
00034 protected $groupConfiguration;
00035
00039 public function testParsing( $messages, $authors, $file ) {
00043 $group = MessageGroupBase::factory( $this->groupConfiguration );
00044 $ffs = new JsonFFS( $group );
00045 $parsed = $ffs->readFromVariable( $file );
00046 $expected = array(
00047 'MESSAGES' => $messages,
00048 'AUTHORS' => $authors,
00049 'METADATA' => array(),
00050 );
00051 $this->assertEquals( $expected, $parsed );
00052
00053 if ( $messages === array() ) {
00054 $this->assertFalse( JsonFFS::isValid( $file ) );
00055 } else {
00056 $this->assertTrue( JsonFFS::isValid( $file ) );
00057 }
00058 }
00059
00060 public function jsonProvider() {
00061 $values = array();
00062
00063 $file1 =
00064 <<<JSON
00065 {
00066 "one": "jeden",
00067 "two": "dwa",
00068 "three": "trzy"
00069 }
00070 JSON;
00071
00072 $values[] = array(
00073 array(
00074 'one' => 'jeden',
00075 'two' => 'dwa',
00076 'three' => 'trzy',
00077 ),
00078 array(),
00079 $file1,
00080 );
00081
00082 $file2 =
00083 <<<JSON
00084 {
00085 "@metadata": {
00086 "authors": ["Niklas", "Amir"]
00087 },
00088 "word": "слово"
00089 }
00090 JSON;
00091
00092 $values[] = array(
00093 array( 'word' => 'слово' ),
00094 array( 'Niklas', 'Amir' ),
00095 $file2,
00096 );
00097
00098 $file3 =
00099 <<<JSON
00100 <This is not
00101 Json!>@£0 file
00102 JSON;
00103
00104 $values[] = array(
00105 array(),
00106 array(),
00107 $file3,
00108 );
00109
00110 return $values;
00111 }
00112
00113 public function testExport() {
00114 $collection = new MockMessageCollectionForExport();
00118 $group = MessageGroupBase::factory( $this->groupConfiguration );
00119 $ffs = new JsonFFS( $group );
00120 $data = $ffs->writeIntoVariable( $collection );
00121 $parsed = $ffs->readFromVariable( $data );
00122
00123 $this->assertEquals(
00124 array( 'Nike the bunny' ),
00125 $parsed['AUTHORS'],
00126 'Authors are exported'
00127 );
00128 $this->assertArrayHasKey( 'fuzzymsg', $parsed['MESSAGES'], 'fuzzy message is exported' );
00129 $this->assertArrayHasKey(
00130 'translatedmsg',
00131 $parsed['MESSAGES'],
00132 'translated message is exported'
00133 );
00134 if ( array_key_exists( 'untranslatedmsg', $parsed['MESSAGES'] ) ) {
00135 $this->fail( 'Untranslated messages should not be exported' );
00136 }
00137
00138 $this->assertEquals(
00139 'metavalue',
00140 $parsed['METADATA']['metakey'],
00141 'metadata is preserved'
00142 );
00143 }
00144 }