d36bb954d1
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
49 lines
1.8 KiB
PHP
49 lines
1.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
require_once dirname( __DIR__, 3 ) . '/includes/export/class-tmdo-csv-writer.php';
|
|
|
|
/**
|
|
* Unit tests for WPDO_CSV_Writer (v2.5.0 M14).
|
|
*/
|
|
class CsvWriterTest extends TestCase {
|
|
|
|
public function test_starts_with_utf8_bom(): void {
|
|
$out = WPDO_CSV_Writer::build( array( 'a', 'b' ), array() );
|
|
$this->assertSame( "\xEF\xBB\xBF", substr( $out, 0, 3 ) );
|
|
}
|
|
|
|
public function test_simple_row_csv_output(): void {
|
|
$out = WPDO_CSV_Writer::build( array( 'a', 'b' ), array( array( 'a' => '1', 'b' => '2' ) ) );
|
|
$this->assertStringContainsString( "a,b\r\n1,2\r\n", $out );
|
|
}
|
|
|
|
public function test_field_with_comma_gets_quoted(): void {
|
|
$out = WPDO_CSV_Writer::build( array( 'col' ), array( array( 'col' => 'foo,bar' ) ) );
|
|
$this->assertStringContainsString( '"foo,bar"', $out );
|
|
}
|
|
|
|
public function test_field_with_quote_doubles_it(): void {
|
|
$out = WPDO_CSV_Writer::build( array( 'col' ), array( array( 'col' => 'say "hi"' ) ) );
|
|
$this->assertStringContainsString( '"say ""hi"""', $out );
|
|
}
|
|
|
|
public function test_field_with_newline_gets_quoted(): void {
|
|
$out = WPDO_CSV_Writer::build( array( 'col' ), array( array( 'col' => "line1\nline2" ) ) );
|
|
$this->assertStringContainsString( "\"line1\nline2\"", $out );
|
|
}
|
|
|
|
public function test_array_value_serializes_to_json(): void {
|
|
$out = WPDO_CSV_Writer::build( array( 'col' ), array( array( 'col' => array( 'a', 'b' ) ) ) );
|
|
// Array becomes JSON; quotes inside JSON are doubled inside the CSV-quoted field.
|
|
$this->assertStringContainsString( '"[""a"",""b""]"', $out );
|
|
}
|
|
|
|
public function test_missing_field_renders_empty(): void {
|
|
$out = WPDO_CSV_Writer::build( array( 'a', 'b' ), array( array( 'a' => '1' ) ) );
|
|
$this->assertStringContainsString( "1,\r\n", $out );
|
|
}
|
|
}
|