bugfix: trustedproxy is not a delimited pcre
[dokuwiki.git] / _test / tests / inc / io_replaceinfile.test.php
blob61b0e12e0d4e9b453e9d1c2b151ebc8f8570c2b4
1 <?php
3 class io_replaceinfile_test extends DokuWikiTest {
5 protected $contents = "The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012";
7 /*
8 * dependency for tests needing zlib extension to pass
9 */
10 public function test_ext_zlib() {
11 if (!DOKU_HAS_GZIP) {
12 $this->markTestSkipped('skipping all zlib tests. Need zlib extension');
13 return;
15 $this->assertTrue(true);
19 * dependency for tests needing zlib extension to pass
21 public function test_ext_bz2() {
22 if (!DOKU_HAS_BZIP) {
23 $this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension');
24 return;
26 $this->assertTrue(true);
29 function _write($file){
31 io_saveFile($file, $this->contents);
32 // Replace one, no regex
33 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete00\012", false, 1));
34 $this->assertEquals("The\012Delete00\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file));
35 // Replace all, no regex
36 $this->assertTrue(io_replaceInFile($file, "Delete\012", "DeleteX\012", false, -1));
37 $this->assertEquals("The\012Delete00\012DeleteX\012Delete01\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file));
38 // Replace two, regex and backreference
39 $this->assertTrue(io_replaceInFile($file, "#Delete(\\d+)\012#", "\\1\012", true, 2));
40 $this->assertEquals("The\01200\012DeleteX\01201\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file));
41 // Delete and insert, no regex
42 $this->assertTrue(io_replaceInFile($file, "DeleteX\012", "Replace\012", false, 0));
43 $this->assertEquals("The\01200\01201\012Delete02\012Test\012Replace\012", io_readFile($file));
46 function test_replace(){
47 $this->_write(TMP_DIR.'/test.txt');
51 /**
52 * @depends test_ext_zlib
54 function test_gzwrite(){
55 $this->_write(TMP_DIR.'/test.txt.gz');
58 /**
59 * @depends test_ext_bz2
61 function test_bzwrite(){
62 $this->_write(TMP_DIR.'/test.txt.bz2');
65 /**
66 * Test for a non-regex replacement where $newline contains a backreference like construct - it shouldn't affect the replacement
68 function test_edgecase1()
70 $file = TMP_DIR . '/test.txt';
72 io_saveFile($file, $this->contents);
73 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\\00\012", false, -1));
74 $this->assertEquals("The\012Delete\\00\012Delete\\00\012Delete01\012Delete02\012Delete\\00\012DeleteX\012Test\012", io_readFile($file), "Edge case: backreference like construct in replacement line");
76 /**
77 * Test with replace all where replacement line == search line - must not timeout
79 * @small
81 function test_edgecase2() {
82 $file = TMP_DIR.'/test.txt';
84 io_saveFile($file, $this->contents);
85 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1));
86 $this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line");
89 /**
90 * Test where $oldline exactly matches one line and also matches part of other lines - only the exact match should be replaced
92 function test_edgecase3()
94 $file = TMP_DIR . '/test.txt';
95 $contents = "The\012Delete\01201Delete\01202Delete\012Test\012";
97 io_saveFile($file, $contents);
98 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1));
99 $this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines");
103 * Test passing an invalid parameter.
105 function test_badparam()
107 if (class_exists('PHPUnit\Framework\Error\Warning')) {
108 $expect = 'PHPUnit\Framework\Error\Warning'; // PHPUnit 6
109 } else {
110 $expect = 'PHPUnit_Framework_Error_Warning'; // PHPUnit 5
112 $this->expectException($expect);
114 /* The empty $oldline parameter should be caught before the file doesn't exist test. */
115 $this->assertFalse(io_replaceInFile(TMP_DIR.'/not_existing_file.txt', '', '', false, 0));