1<?php 2 3class io_replaceinfile_test extends DokuWikiTest { 4 5 protected $contents = "The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012"; 6 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 } 14 } 15 16 /* 17 * dependency for tests needing zlib extension to pass 18 */ 19 public function test_ext_bz2() { 20 if (!DOKU_HAS_BZIP) { 21 $this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension'); 22 } 23 } 24 25 function _write($file){ 26 27 io_saveFile($file, $this->contents); 28 // Replace one, no regex 29 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete00\012", false, 1)); 30 $this->assertEquals("The\012Delete00\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file)); 31 // Replace all, no regex 32 $this->assertTrue(io_replaceInFile($file, "Delete\012", "DeleteX\012", false, -1)); 33 $this->assertEquals("The\012Delete00\012DeleteX\012Delete01\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file)); 34 // Replace two, regex and backreference 35 $this->assertTrue(io_replaceInFile($file, "#Delete(\\d+)\012#", "\\1\012", true, 2)); 36 $this->assertEquals("The\01200\012DeleteX\01201\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file)); 37 // Delete and insert, no regex 38 $this->assertTrue(io_replaceInFile($file, "DeleteX\012", "Replace\012", false, 0)); 39 $this->assertEquals("The\01200\01201\012Delete02\012Test\012Replace\012", io_readFile($file)); 40 } 41 42 function test_replace(){ 43 $this->_write(TMP_DIR.'/test.txt'); 44 } 45 46 47 /** 48 * @depends test_ext_zlib 49 */ 50 function test_gzwrite(){ 51 $this->_write(TMP_DIR.'/test.txt.gz'); 52 } 53 54 /** 55 * @depends test_ext_bz2 56 */ 57 function test_bzwrite(){ 58 $this->_write(TMP_DIR.'/test.txt.bz2'); 59 } 60 61 /** 62 * Test for a non-regex replacement where $newline contains a backreference like construct - it shouldn't affect the replacement 63 */ 64 function test_edgecase1() 65 { 66 $file = TMP_DIR . '/test.txt'; 67 68 io_saveFile($file, $this->contents); 69 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\\00\012", false, -1)); 70 $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"); 71 } 72 /** 73 * Test with replace all where replacement line == search line - must not timeout 74 * 75 * @small 76 */ 77 function test_edgecase2() { 78 $file = TMP_DIR.'/test.txt'; 79 80 io_saveFile($file, $this->contents); 81 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1)); 82 $this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line"); 83 } 84 85 /** 86 * Test where $oldline exactly matches one line and also matches part of other lines - only the exact match should be replaced 87 */ 88 function test_edgecase3() 89 { 90 $file = TMP_DIR . '/test.txt'; 91 $contents = "The\012Delete\01201Delete\01202Delete\012Test\012"; 92 93 io_saveFile($file, $contents); 94 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1)); 95 $this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines"); 96 } 97 98 /** 99 * Test passing an invalid parameter. 100 */ 101 function test_badparam() 102 { 103 if (class_exists('PHPUnit\Framework\Error\Warning')) { 104 $expect = PHPUnit\Framework\Error\Warning::class; // PHPUnit 6 105 } else { 106 $expect = PHPUnit_Framework_Error_Warning::class; // PHPUnit 5 107 } 108 $this->setExpectedException($expect); 109 110 /* The empty $oldline parameter should be caught before the file doesn't exist test. */ 111 $this->assertFalse(io_replaceInFile(TMP_DIR.'/not_existing_file.txt', '', '', false, 0)); 112 } 113} 114