1<?php
2/**
3 * General tests for the uncmap plugin
4 *
5 * @group plugin_uncmap
6 * @group plugins
7 */
8require_once 'uncmap.inc.php';
9
10class mapping_plugin_uncmap_test extends uncmapDokuWikiTest {
11
12    protected $pluginsEnabled = array('uncmap');
13
14    function setUp(){
15        parent::setUp();
16        TestUtils::rcopy(TMP_DIR, dirname(__FILE__).'/../conf/mapping.php');
17        TestUtils::rdelete(dirname(__FILE__).'/../conf/mapping.php');
18        touch(dirname(__FILE__).'/../conf/mapping.php');
19        TestUtils::fappend(dirname(__FILE__).'/../conf/mapping.php','z           \\\\server1\\documents'.PHP_EOL);
20        TestUtils::fappend(dirname(__FILE__).'/../conf/mapping.php','x   \\\\server2\\shareTest   '.TMP_DIR.PHP_EOL);
21        TestUtils::fappend(dirname(__FILE__).'/../conf/mapping.php','k           \\\\server3\\documentsTest'.PHP_EOL);
22        mkdir(TMP_DIR.'/shareTest');
23        touch(TMP_DIR.'/shareTest/testfile.tmp');
24        mkdir(TMP_DIR.'/documentsTest');
25        touch(TMP_DIR.'/documentsTest/testdefaultfile.tmp');
26    }
27
28    function tearDown(){
29        parent::tearDown();
30        if(file_exists(TMP_DIR.'/mapping.php')) {
31            TestUtils::rdelete(dirname(__FILE__) . '/../conf/mapping.php');
32            TestUtils::rcopy(dirname(__FILE__).'/../conf', TMP_DIR.'/mapping.php');
33        }
34        TestUtils::rdelete(TMP_DIR.'/shareTest');
35        TestUtils::rdelete(TMP_DIR.'/documentsTest');
36    }
37
38
39    function test_parser_mapping() {
40        $parser_response = p_get_instructions('Testlink: [[z:/path/to/file]]');
41        $parser_response = $this->flatten_array($parser_response);
42        $uncmap_pos = array_search("uncmap",$parser_response,true);
43        $this->assertTrue($uncmap_pos !== false,'uncmap should be invoked');
44        $link_pos = array_search("\\\\server1\\documents\\path\\to\\file",$parser_response,true);
45        $this->assertTrue($link_pos !== false,'the link is not mapped correctly');
46    }
47
48    function test_upper_parser_mapping() {
49        $parser_response = p_get_instructions('Testlink: [[Z:/path/to/file]]');
50        $parser_response = $this->flatten_array($parser_response);
51        $uncmap_pos = array_search("uncmap",$parser_response,true);
52        $this->assertTrue($uncmap_pos !== false,'uncmap should be invoked');
53        $link_pos = array_search("\\\\server1\\documents\\path\\to\\file",$parser_response,true);
54        $this->assertTrue($link_pos !== false,'the link is not mapped correctly');
55    }
56
57    function test_parser_mapping_with_title() {
58        $parser_response = p_get_instructions('Testlink: [[z:/path/to/file|some title]]');
59        $parser_response = $this->flatten_array($parser_response);
60        $uncmap_pos = array_search("uncmap",$parser_response,true);
61        $this->assertTrue($uncmap_pos !== false,'uncmap should be invoked');
62        $link_pos = array_search("\\\\server1\\documents\\path\\to\\file",$parser_response,true);
63        $this->assertTrue($link_pos !== false,'the link is not mapped correctly');
64        $this->assertTrue($parser_response[$link_pos-1] == 'some title','title not recognized correctly');
65    }
66
67    function test_parser_no_mapping() {
68        $parser_response = p_get_instructions('Testlink: [[y:/path/to/file]]');
69        $parser_response = $this->flatten_array($parser_response);
70        $uncmap_pos = array_search("uncmap",$parser_response,true);
71        $this->assertTrue($uncmap_pos === false,'uncmap should not be invoked');
72    }
73
74    function test_parser_colon() {
75        $parser_response = p_get_instructions('Testlink: [[:facts:figures|Foo]]');
76        $parser_response = $this->flatten_array($parser_response);
77        $uncmap_pos = array_search("uncmap",$parser_response,true);
78        $this->assertTrue($uncmap_pos === false,'A link beginning with a colon should not be handled at all by this plugin.');
79    }
80
81    function test_output_title() {
82        global $ID;
83        $ID = 'wiki:start';
84        $request = new TestRequest();
85        $input = array(
86            'id' => 'wiki:start'
87        );
88        saveWikiText('wiki:start', 'Testlink: [[z:/path/to/file|some title]]', 'Test initialization');
89        $response = $request->post($input);
90        $this->assertTrue(
91            strpos($response->getContent(), 'Testlink') !== false,
92            'This tests the test and should always succeed.'
93        );
94        $this->assertTrue(
95            strpos($response->getContent(), '>some title</a>') !== false,
96            'The title is incorrect.'
97        );
98        $this->assertTrue(
99            strpos($response->getContent(), 'href="file:///server1/documents/path/to/file"') !== false,
100            'The url is incorrect.'
101        );
102        $this->assertTrue(
103            strpos($response->getContent(), 'class="windows"') !== false,
104            'The class for a link without local fileserver is incorrect.'
105        );
106    }
107
108    function test_output_file_exists() {
109        global $ID;
110        $ID = 'wiki:start';
111        $request = new TestRequest();
112        $input = array(
113            'id' => 'wiki:start'
114        );
115        saveWikiText('wiki:start', 'Testlink: [[x:/testfile.tmp|existing file]]', 'Test initialization');
116        $response = $request->post($input);
117        $this->assertTrue(
118            strpos($response->getContent(), 'Testlink') !== false,
119            'This tests the test and should always succeed.'
120        );
121        $this->assertTrue(
122            strpos($response->getContent(), 'class="wikilink1"') !== false,
123            'The class for an existing link is incorrect.'
124        );
125        $this->assertTrue(
126            strpos($response->getContent(), 'href="file:///server2/shareTest/testfile.tmp"') !== false,
127            'The url is incorrect.'
128        );
129    }
130
131    function test_output_file_not_exists() {
132        global $ID;
133        $ID = 'wiki:start';
134        $request = new TestRequest();
135        $input = array(
136            'id' => 'wiki:start'
137        );
138        saveWikiText('wiki:start', 'Testlink: [[x:/notestfile.tmp|not existing file]]', 'Test initialization');
139        $response = $request->post($input);
140        $this->assertTrue(
141            strpos($response->getContent(), 'Testlink') !== false,
142            'This tests the test and should always succeed.'
143        );
144        $this->assertTrue(
145            strpos($response->getContent(), 'class="wikilink2"') !== false,
146            'The class for an non-existing link is incorrect.'
147        );
148        $this->assertTrue(
149            strpos($response->getContent(), 'href="file:///server2/shareTest/notestfile.tmp"') !== false,
150            'The url is incorrect.'
151        );
152    }
153
154    function test_output_file_exists_default() {
155        global $ID, $conf;
156
157        $conf['plugin']['uncmap']['fileserver'] = TMP_DIR;
158        $ID = 'wiki:start';
159        $request = new TestRequest();
160        $input = array(
161            'id' => 'wiki:start'
162        );
163        saveWikiText('wiki:start', 'Testlink: [[k:/testdefaultfile.tmp|file exists at default fs]]', 'Test init');
164        $response = $request->post($input);
165        $this->assertTrue(
166            strpos($response->getContent(), 'Testlink') !== false,
167            'This tests the test and should always succeed.'
168        );
169        $this->assertTrue(
170            strpos($response->getContent(), 'href="file:///server3/documentsTest/testdefaultfile.tmp"') !== false,
171            'The url is incorrect.'
172        );
173        $this->assertTrue(
174            strpos($response->getContent(), 'class="wikilink1"') !== false,
175            'The class for a link that exists at the default fileserver is incorrect.'
176        );
177    }
178
179}
180