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            return;
14        }
15        $this->assertTrue(true);
16    }
17
18    /*
19     * dependency for tests needing zlib extension to pass
20     */
21    public function test_ext_bz2() {
22        if (!DOKU_HAS_BZIP) {
23            $this->markTestSkipped('skipping all bzip2 tests.  Need bz2 extension');
24            return;
25        }
26        $this->assertTrue(true);
27    }
28
29    function _write($file){
30
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));
44    }
45
46    function test_replace(){
47        $this->_write(TMP_DIR.'/test.txt');
48    }
49
50
51    /**
52     * @depends test_ext_zlib
53     */
54    function test_gzwrite(){
55        $this->_write(TMP_DIR.'/test.txt.gz');
56    }
57
58    /**
59     * @depends test_ext_bz2
60     */
61    function test_bzwrite(){
62        $this->_write(TMP_DIR.'/test.txt.bz2');
63    }
64
65    /**
66     * Test for a non-regex replacement where $newline contains a backreference like construct - it shouldn't affect the replacement
67     */
68    function test_edgecase1()
69    {
70        $file = TMP_DIR . '/test.txt';
71
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");
75    }
76    /**
77     * Test with replace all where replacement line == search line - must not timeout
78     *
79     * @small
80     */
81    function test_edgecase2() {
82        $file = TMP_DIR.'/test.txt';
83
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");
87    }
88
89    /**
90     *    Test where $oldline exactly matches one line and also matches part of other lines - only the exact match should be replaced
91     */
92    function test_edgecase3()
93    {
94        $file = TMP_DIR . '/test.txt';
95        $contents = "The\012Delete\01201Delete\01202Delete\012Test\012";
96
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");
100    }
101
102    /**
103     * Test passing an invalid parameter.
104     */
105    function test_badparam()
106    {
107        $this->expectLogMessage('io_replaceInFile() $oldline parameter cannot be empty');
108
109        /* The empty $oldline parameter should be caught before the file doesn't exist test. */
110        $this->assertFalse(io_replaceInFile(TMP_DIR.'/not_existing_file.txt', '', '', false, 0));
111    }
112}
113