|
| 1 | +<?php |
| 2 | +/** |
| 3 | +* |
| 4 | +* PhpBB Gallery extension for the phpBB Forum Software package. |
| 5 | +* |
| 6 | +* @copyright (c) 2015 Lucifer <https://www.anavaro.com> |
| 7 | +* @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | +* |
| 9 | +*/ |
| 10 | + |
| 11 | +namespace phpbbgallery\tests\core; |
| 12 | +/** |
| 13 | +* @group core |
| 14 | +*/ |
| 15 | +require_once dirname(__FILE__) . '/../../../../includes/functions.php'; |
| 16 | + |
| 17 | +class core_url_test extends core_base |
| 18 | +{ |
| 19 | + /** @var \phpbbgallery\core\url */ |
| 20 | + protected $url; |
| 21 | + |
| 22 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\template\template */ |
| 23 | + protected $template; |
| 24 | + |
| 25 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\request\request */ |
| 26 | + protected $request; |
| 27 | + |
| 28 | + /** @var \phpbb\config\config */ |
| 29 | + protected $config; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + parent::setUp(); |
| 34 | + |
| 35 | + global $phpbb_root_path, $phpEx, $config; |
| 36 | + |
| 37 | + $this->template = $this->getMockBuilder('\\phpbb\\template\\template') |
| 38 | + ->getMock(); |
| 39 | + |
| 40 | + $this->request = $this->getMockBuilder('\\phpbb\\request\\request') |
| 41 | + ->disableOriginalConstructor() |
| 42 | + ->getMock(); |
| 43 | + |
| 44 | + $this->config = new \phpbb\config\config(array( |
| 45 | + 'server_name' => 'localhost', |
| 46 | + 'server_protocol' => 'http://', |
| 47 | + 'server_port' => 80, |
| 48 | + 'force_server_vars' => 0, |
| 49 | + )); |
| 50 | + |
| 51 | + $this->url = new \phpbbgallery\core\url( |
| 52 | + $this->template, |
| 53 | + $this->request, |
| 54 | + $this->config, |
| 55 | + $phpbb_root_path, |
| 56 | + $phpEx |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_path() |
| 61 | + { |
| 62 | + $this->assertStringContainsString('gallery', $this->url->path('gallery')); |
| 63 | + $this->assertStringContainsString('ext/phpbbgallery/core', $this->url->path('ext')); |
| 64 | + $this->assertStringContainsString('phpBB', $this->url->path('phpbb')); |
| 65 | + $this->assertStringContainsString('adm', $this->url->path('admin')); |
| 66 | + $this->assertStringContainsString('gallery', $this->url->path('relative')); |
| 67 | + $this->assertStringContainsString('http', $this->url->path('full')); |
| 68 | + $this->assertStringContainsString('http', $this->url->path('board')); |
| 69 | + $this->assertStringContainsString('images', $this->url->path('images')); |
| 70 | + $this->assertStringContainsString('files/phpbbgallery', $this->url->path('upload')); |
| 71 | + $this->assertStringContainsString('thumbnail', $this->url->path('thumbnail')); |
| 72 | + $this->assertStringContainsString('medium', $this->url->path('medium')); |
| 73 | + $this->assertStringContainsString('import', $this->url->path('import')); |
| 74 | + $this->assertStringContainsString('upload', $this->url->path('upload_noroot')); |
| 75 | + $this->assertStringContainsString('thumbnail', $this->url->path('thumbnail_noroot')); |
| 76 | + $this->assertStringContainsString('medium', $this->url->path('medium_noroot')); |
| 77 | + $this->assertStringContainsString('import', $this->url->path('import_noroot')); |
| 78 | + $this->assertFalse($this->url->path('invalid_path')); |
| 79 | + } |
| 80 | + |
| 81 | + public function test_phpEx_file() |
| 82 | + { |
| 83 | + $this->assertEquals('test.php', $this->url->phpEx_file('test')); |
| 84 | + $this->assertEquals('test.php', $this->url->phpEx_file('test.php')); |
| 85 | + $this->assertEquals('test/', $this->url->phpEx_file('test/')); |
| 86 | + $this->assertEquals('', $this->url->phpEx_file('')); |
| 87 | + } |
| 88 | + |
| 89 | + public function test_show_image() |
| 90 | + { |
| 91 | + $image_id = 123; |
| 92 | + $result = $this->url->show_image($image_id); |
| 93 | + $this->assertStringContainsString((string)$image_id, $result); |
| 94 | + $this->assertStringContainsString('medium', $result); |
| 95 | + |
| 96 | + $custom_size = 'large'; |
| 97 | + $result = $this->url->show_image($image_id, $custom_size); |
| 98 | + $this->assertStringContainsString($custom_size, $result); |
| 99 | + } |
| 100 | + |
| 101 | + public function test_show_album() |
| 102 | + { |
| 103 | + $album_id = 456; |
| 104 | + $result = $this->url->show_album($album_id); |
| 105 | + $this->assertStringContainsString((string)$album_id, $result); |
| 106 | + } |
| 107 | + |
| 108 | + public function test_beautiful_path() |
| 109 | + { |
| 110 | + // Test with relative path |
| 111 | + $path = '../community/../gallery/'; |
| 112 | + $expected = '../gallery/'; |
| 113 | + $this->assertEquals($expected, \phpbbgallery\core\url::beautiful_path($path)); |
| 114 | + |
| 115 | + // Test with full URL |
| 116 | + $url = 'http://example.com/community/../gallery/'; |
| 117 | + $expected = 'http://example.com/gallery/'; |
| 118 | + $this->assertEquals($expected, \phpbbgallery\core\url::beautiful_path($url, true)); |
| 119 | + |
| 120 | + // Test with https |
| 121 | + $url = 'https://example.com/community/../gallery/'; |
| 122 | + $expected = 'https://example.com/gallery/'; |
| 123 | + $this->assertEquals($expected, \phpbbgallery\core\url::beautiful_path($url, true)); |
| 124 | + } |
| 125 | + |
| 126 | + public function test_meta_refresh() |
| 127 | + { |
| 128 | + $time = 5; |
| 129 | + $route = 'gallery/album/1'; |
| 130 | + |
| 131 | + $this->template->expects($this->once()) |
| 132 | + ->method('assign_vars') |
| 133 | + ->with($this->callback(function($vars) use ($time, $route) { |
| 134 | + return strpos($vars['META'], (string)$time) !== false && |
| 135 | + strpos($vars['META'], $route) !== false; |
| 136 | + })); |
| 137 | + |
| 138 | + $this->url->meta_refresh($time, $route); |
| 139 | + } |
| 140 | + |
| 141 | + public function test_get_uri() |
| 142 | + { |
| 143 | + $route = '/gallery/album/1'; |
| 144 | + $result = $this->url->get_uri($route); |
| 145 | + $this->assertStringStartsWith('http://', $result); |
| 146 | + $this->assertStringEndsWith($route, $result); |
| 147 | + $this->assertStringContainsString('localhost', $result); |
| 148 | + |
| 149 | + // Test with HTTPS |
| 150 | + $this->request->expects($this->any()) |
| 151 | + ->method('server') |
| 152 | + ->with('HTTPS', '') |
| 153 | + ->willReturn('on'); |
| 154 | + |
| 155 | + $result = $this->url->get_uri($route); |
| 156 | + $this->assertStringStartsWith('https://', $result); |
| 157 | + } |
| 158 | + |
| 159 | + public function test_append_sid() |
| 160 | + { |
| 161 | + // Test basic URL |
| 162 | + $result = $this->url->append_sid('index', 'mode=view'); |
| 163 | + $this->assertStringContainsString('index.php', $result); |
| 164 | + $this->assertStringContainsString('mode=view', $result); |
| 165 | + |
| 166 | + // Test with path prefix |
| 167 | + $result = $this->url->append_sid('phpbb', 'app.php/foo/bar'); |
| 168 | + $this->assertStringContainsString('app.php/foo/bar', $result); |
| 169 | + |
| 170 | + // Test with array parameters |
| 171 | + $result = $this->url->append_sid(array('index', 'mode=view&id=1')); |
| 172 | + $this->assertStringContainsString('index.php', $result); |
| 173 | + $this->assertStringContainsString('mode=view', $result); |
| 174 | + $this->assertStringContainsString('id=1', $result); |
| 175 | + } |
| 176 | + |
| 177 | + public function test_create_link() |
| 178 | + { |
| 179 | + $path = 'gallery'; |
| 180 | + $file = 'index'; |
| 181 | + $params = 'album_id=1&image_id=2'; |
| 182 | + |
| 183 | + $result = $this->url->create_link($path, $file, $params); |
| 184 | + $this->assertStringContainsString($file . '.php', $result); |
| 185 | + $this->assertStringContainsString($params, $result); |
| 186 | + |
| 187 | + // Test with array parameters |
| 188 | + $paramsArray = array('album_id' => 1, 'image_id' => 2); |
| 189 | + $result = $this->url->create_link($path, $file, $paramsArray, false); |
| 190 | + $this->assertStringContainsString($file . '.php', $result); |
| 191 | + $this->assertStringContainsString('album_id=1', $result); |
| 192 | + $this->assertStringContainsString('image_id=2', $result); |
| 193 | + } |
| 194 | + |
| 195 | + public function test_include_methods() |
| 196 | + { |
| 197 | + $path = 'gallery'; |
| 198 | + $file = 'test_include'; |
| 199 | + $sub_directory = 'includes/'; |
| 200 | + |
| 201 | + // Test _return_file |
| 202 | + $result = $this->url->_return_file($file, $path, $sub_directory); |
| 203 | + $this->assertStringContainsString($file . '.php', $result); |
| 204 | + $this->assertStringContainsString($sub_directory, $result); |
| 205 | + |
| 206 | + // Test _file_exists |
| 207 | + $this->assertIsBool($this->url->_file_exists($file, $path, $sub_directory)); |
| 208 | + |
| 209 | + // Test _is_writable |
| 210 | + $this->assertIsBool($this->url->_is_writable($file, $path, $sub_directory)); |
| 211 | + } |
| 212 | +} |
0 commit comments