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 (!extension_loaded('zlib')) { 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 (!extension_loaded('bz2')) { 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 * 63 */ 64 function test_edgecase1() 65 { 66 $file = TMP_DIR . '/test.txt'; 67 // Replace all, no regex, backreference like construct in replacement line 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 * @small 74 */ 75 function test_edgecase2() { 76 $file = TMP_DIR.'/test.txt'; 77 // Replace all, no regex, replacement line == search line 78 io_saveFile($file, $this->contents); 79 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1)); 80 $this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line"); 81 } 82 83 /** 84 * 85 */ 86 function test_edgecase3() 87 { 88 $file = TMP_DIR . '/test.txt'; 89 $contents = "The\012Delete\01201Delete\01202Delete\012Test\012"; 90 // Replace all, no regex, oldline exactly matches one line; matches part of other lines - only the exact match should be replaced 91 io_saveFile($file, $contents); 92 $this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1)); 93 $this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines"); 94 } 95} 96