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89C8;AM|:	
TF;Fk 5A:]х8)cYg{.:j#fjH)$m鵓ol_~xחNix	C+H&|}EtB[."lW!ŅnC@J`<%qa~2srC@b\ZzS|yMmplÔR:7=(%1
 /M[s]=<Y`2X	p(` 8HT$EQfo8(6sqX(*zFp?ժ$LfRy[!՗ 	K#[d]9nt.@%b)-EYm10$@y;#j[CnteOQ$VQ}l1{X׾RK Mb^^>pI%)-iǶ5kR
ʈ[cbˢCӶ0+>fMxb^9-yYCN]HXik%Bܻ%e@M,?X^ϚeJOo'kWW7@uU^ (\<WH1sTA<e%jn!	B
QҎ-`6`Oq,JZBrdqdXvLrTqK@<ծފH<g!`A&tfq6SgyO;0qs	=xj,Feee|z[bjms1F'Z1p<h{Z.q4\7b2^[$:];S,wkr4)<Fb<7)rL6}H$t]j)Ț^Wgiiƒ|UW{Etm7=(%1
 /M[sok^8]N%Za]^*RRkW~n||ԐB@V8:<.]h0֞E QDwze@Ça-2KOxGۃc=#QL7`]ZM6fF!+.44>D>~E||ԐB@V8:ߘQ_K(YE QDwzeN[SbmvpBO_*Y	yׄ _FtP-왋J@N{PG,1m46`FykT(I̚A? fXll .Ԙe;7 	(M^$: fUދKP &:8c(!PnOS)7(ՙ	d9E QDwze@Ça-2KOxGۃc=#QL{NpH0zr]V:qaT՜i&V: 0ˤukIl%J\Yl6?HWMY,ڒVZ\ghh4ȞiF.44>D>~E||ԐB@V8:ߘQ_K(YE QDwzeN[SbmvpBO_*Y	yׄ _FtP-왋J@N{PG,1m46`FykT(I̚A? fXll .Ԙe;7 	(M^$: fUދKP &:8c(!PnOS)7 $property
        );

        $this->assertEquals(
            'terminated',
            $property->getName()
        );

        $this->assertEquals(
            'Lines terminated with:',
            $property->getText()
        );

        $property = array_shift($generalProperties);

        $this->assertInstanceOf(
            'PMA\libraries\properties\options\items\TextPropertyItem',
            $property
        );

        $this->assertEquals(
            'null',
            $property->getName()
        );

        $this->assertEquals(
            'Replace NULL with:',
            $property->getText()
        );

        $property = array_shift($generalProperties);

        $this->assertInstanceOf(
            'PMA\libraries\properties\options\items\BoolPropertyItem',
            $property
        );

        $this->assertEquals(
            'removeCRLF',
            $property->getName()
        );

        $this->assertEquals(
            'Remove carriage return/line feed characters within columns',
            $property->getText()
        );

        $property = array_shift($generalProperties);

        $this->assertInstanceOf(
            'PMA\libraries\properties\options\items\BoolPropertyItem',
            $property
        );

        $this->assertEquals(
            'columns',
            $property->getName()
        );

        $this->assertEquals(
            'Put columns names in the first row',
            $property->getText()
        );

        $property = array_shift($generalProperties);

        $this->assertInstanceOf(
            'PMA\libraries\properties\options\items\HiddenPropertyItem',
            $property
        );

        $this->assertEquals(
            'structure_or_data',
            $property->getName()
        );

    }

    /**
     * Test for PMA\libraries\plugins\export\ExportCsv::exportHeader
     *
     * @return void
     */
    public function testExportHeader()
    {
        // case 1

        $GLOBALS['what'] = 'excel';
        $GLOBALS['excel_edition'] = 'win';
        $GLOBALS['excel_columns'] = true;

        $this->assertTrue(
            $this->object->exportHeader()
        );

        $this->assertEquals(
            "\015\012",
            $GLOBALS['csv_terminated']
        );

        $this->assertEquals(
            ";",
            $GLOBALS['csv_separator']
        );

        $this->assertEquals(
            '"',
            $GLOBALS['csv_enclosed']
        );

        $this->assertEquals(
            '"',
            $GLOBALS['csv_escaped']
        );

        $this->assertEquals(
            'yes',
            $GLOBALS['csv_columns']
        );

        // case 2

        $GLOBALS['excel_edition'] = 'mac_excel2003';
        unset($GLOBALS['excel_columns']);
        $GLOBALS['csv_columns'] = 'no';

        $this->assertTrue(
            $this->object->exportHeader()
        );

        $this->assertEquals(
            "\015\012",
            $GLOBALS['csv_terminated']
        );

        $this->assertEquals(
            ";",
            $GLOBALS['csv_separator']
        );

        $this->assertEquals(
            '"',
            $GLOBALS['csv_enclosed']
        );

        $this->assertEquals(
            '"',
            $GLOBALS['csv_escaped']
        );

        $this->assertEquals(
            'no',
            $GLOBALS['csv_columns']
        );

        // case 3

        $GLOBALS['excel_edition'] = 'mac_excel2008';

        $this->assertTrue(
            $this->object->exportHeader()
        );

        $this->assertEquals(
            "\015\012",
            $GLOBALS['csv_terminated']
        );

        $this->assertEquals(
            ",",
            $GLOBALS['csv_separator']
        );

        $this->assertEquals(
            '"',
            $GLOBALS['csv_enclosed']
        );

        $this->assertEquals(
            '"',
            $GLOBALS['csv_escaped']
        );

        $this->assertEquals(
            'no',
            $GLOBALS['csv_columns']
        );

        // case 4

        $GLOBALS['excel_edition'] = 'testBlank';
        $GLOBALS['csv_separator'] = '#';

        $this->assertTrue(
            $this->object->exportHeader()
        );

        $this->assertEquals(
            '#',
            $GLOBALS['csv_separator']
        );

        // case 5

        $GLOBALS['what'] = 'notExcel';
        $GLOBALS['crlf'] = "\n";
        $GLOBALS['csv_terminated'] = '';
        $GLOBALS['csv_separator'] = 'a\\t';

        $this->assertTrue(
            $this->object->exportHeader()
        );

        $this->assertEquals(
            $GLOBALS['csv_terminated'],
            "\n"
        );

        $this->assertEquals(
            $GLOBALS['csv_separator'],
            "a\011"
        );
        // case 6

        $GLOBALS['csv_terminated'] = 'AUTO';

        $this->assertTrue(
            $this->object->exportHeader()
        );

        $this->assertEquals(
            $GLOBALS['csv_terminated'],
            "\n"
        );

        // case 7

        $GLOBALS['csv_terminated'] = 'a\\rb\\nc\\t';
        $GLOBALS['csv_separator'] = 'a\\t';

        $this->assertTrue(
            $this->object->exportHeader()
        );

        $this->assertEquals(
            $GLOBALS['csv_terminated'],
            "a\015b\012c\011"
        );

        $this->assertEquals(
            $GLOBALS['csv_separator'],
            "a\011"
        );
    }

    /**
     * Test for PMA\libraries\plugins\export\ExportCsv::exportFooter
     *
     * @return void
     */
    public function testExportFooter()
    {
        $this->assertTrue(
            $this->object->exportFooter()
        );
    }

    /**
     * Test for PMA\libraries\plugins\export\ExportCsv::exportDBHeader
     *
     * @return void
     */
    public function testExportDBHeader()
    {
        $this->assertTrue(
            $this->object->exportDBHeader('testDB')
        );
    }

    /**
     * Test for PMA\libraries\plugins\export\ExportCsv::exportDBFooter
     *
     * @return void
     */
    public function testExportDBFooter()
    {
        $this->assertTrue(
            $this->object->exportDBFooter('testDB')
        );
    }

    /**
     * Test for PMA\libraries\plugins\export\ExportCsv::exportDBCreate
     *
     * @return void
     */
    public function testExportDBCreate()
    {
        $this->assertTrue(
            $this->object->exportDBCreate('testDB', 'database')
        );
    }

    /**
     * Test for PMA\libraries\plugins\export\ExportCsv::exportData
     *
     * @return void
     */
    public function testExportData()
    {
        // case 1
        $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $GLOBALS['dbi'] = $dbi;

        $GLOBALS['csv_columns'] = 'yes';
        $GLOBALS['csv_terminated'] = ';';

        $GLOBALS['output_kanji_conversion'] = false;
        $GLOBALS['output_charset_conversion'] = false;
        $GLOBALS['buffer_needed'] = false;
        $GLOBALS['asfile'] = true;
        $GLOBALS['save_on_server'] = true;
        $GLOBALS['file_handle'] = null;

        ob_start();
        $this->assertFalse(
            $this->object->exportData(
                'testDB', 'testTable', "\n", 'example.com', 'test'
            )
        );
        $result = ob_get_clean();

        // case 2
        $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();

        $dbi->expects($this->once())
            ->method('query')
            ->with('test', null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED)
            ->will($this->returnValue(true));

        $dbi->expects($this->once())
            ->method('numFields')
            ->with(true)
            ->will($this->returnValue(1));

        $dbi->expects($this->once())
            ->method('fieldName')
            ->with(true, 0)
            ->will($this->returnValue("foo'\\bar"));

        $dbi->expects($this->at(3))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(array(null, 'b', 'c', false, 'e', 'f')));

        $dbi->expects($this->at(4))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(null));
        $GLOBALS['dbi'] = $dbi;

        $GLOBALS['what'] = 'UT';
        $GLOBALS['UT_null'] = 'customNull';
        $GLOBALS['output_kanji_conversion'] = false;
        $GLOBALS['output_charset_conversion'] = false;
        $GLOBALS['buffer_needed'] = false;
        $GLOBALS['asfile'] = true;
        $GLOBALS['save_on_server'] = false;

        ob_start();
        $this->assertTrue(
            $this->object->exportData(
                'testDB', 'testTable', "\n", 'example.com', 'test'
            )
        );
        $result = ob_get_clean();

        $this->assertEquals(
            "foo'ba;customNull;",
            $result
        );

        // case 3

        $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();

        $dbi->expects($this->once())
            ->method('query')
            ->with('test', null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED)
            ->will($this->returnValue(true));

        $dbi->expects($this->once())
            ->method('numFields')
            ->with(true)
            ->will($this->returnValue(1));

        $dbi->expects($this->once())
            ->method('fieldName')
            ->with(true, 0)
            ->will($this->returnValue("foo\"\\bar"));

        $dbi->expects($this->at(3))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(array(1 => 'a')));

        $dbi->expects($this->at(4))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(null));
        $GLOBALS['dbi'] = $dbi;

        $GLOBALS['csv_enclosed'] = '"';

        ob_start();
        $this->assertTrue(
            $this->object->exportData(
                'testDB', 'testTable', "\n", 'example.com', 'test'
            )
        );
        $result = ob_get_clean();

        $this->assertEquals(
            "\"foo\"bar;customNull;",
            $result
        );

        // case 4

        $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();

        $dbi->expects($this->once())
            ->method('query')
            ->with('test', null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED)
            ->will($this->returnValue(true));

        $dbi->expects($this->once())
            ->method('numFields')
            ->with(true)
            ->will($this->returnValue(1));

        $dbi->expects($this->once())
            ->method('fieldName')
            ->with(true, 0)
            ->will($this->returnValue("foo\"\\bar"));

        $dbi->expects($this->at(3))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(array("test\015\012\n")));

        $dbi->expects($this->at(4))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(null));
        $GLOBALS['dbi'] = $dbi;

        $GLOBALS['csv_enclosed'] = '"';
        $GLOBALS['what'] = 'excel';
        $GLOBALS['excel_removeCRLF'] = true;
        $GLOBALS['csv_escaped'] = '"';

        ob_start();
        $this->assertTrue(
            $this->object->exportData(
                'testDB', 'testTable', "\n", 'example.com', 'test'
            )
        );
        $result = ob_get_clean();

        $this->assertEquals(
            "\"foo\"\"bar;\"test\";",
            $result
        );

        // case 5

        $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();

        $dbi->expects($this->once())
            ->method('query')
            ->with('test', null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED)
            ->will($this->returnValue(true));

        $dbi->expects($this->once())
            ->method('numFields')
            ->with(true)
            ->will($this->returnValue(1));

        $dbi->expects($this->once())
            ->method('fieldName')
            ->with(true, 0)
            ->will($this->returnValue("foo\"\\bar"));

        $dbi->expects($this->at(3))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(array("test\015\n")));

        $dbi->expects($this->at(4))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(null));
        $GLOBALS['dbi'] = $dbi;

        $GLOBALS['csv_enclosed'] = '"';
        unset($GLOBALS['excel_removeCRLF']);
        $GLOBALS['csv_escaped'] = ';';

        ob_start();
        $this->assertTrue(
            $this->object->exportData(
                'testDB', 'testTable', "\n", 'example.com', 'test'
            )
        );
        $result = ob_get_clean();

        $this->assertEquals(
            "\"foo;\"bar;\"test\n\";",
            $result
        );

        // case 6

        $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();

        $dbi->expects($this->once())
            ->method('query')
            ->with('test', null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED)
            ->will($this->returnValue(true));

        $dbi->expects($this->once())
            ->method('numFields')
            ->with(true)
            ->will($this->returnValue(2));

        $dbi->expects($this->any())
            ->method('fieldName')
            ->will($this->returnValue("foo\"\\bar"));

        $dbi->expects($this->at(4))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(array("test\015\n", "test\n")));

        $dbi->expects($this->at(5))
            ->method('fetchRow')
            ->with(true)
            ->will($this->returnValue(null));
        $GLOBALS['dbi'] = $dbi;

        $GLOBALS['csv_enclosed'] = '"';
        $GLOBALS['csv_escaped'] = ';';
        $GLOBALS['csv_escaped'] = '#';

        ob_start();
        $this->assertTrue(
            $this->object->exportData(
                'testDB', 'testTable', "\n", 'example.com', 'test'
            )
        );
        $result = ob_get_clean();

        $this->assertEquals(
            "\"foo#\"bar\"\"foo#\"bar;\"test\n" .
            "\"\"test\n" .
            "\";",
            $result
        );
    }
}
