mockApplication(); } public function testNormalizePathInfo() { $normalizer = new UrlNormalizer(); $this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/a/')); $this->assertEquals('post/123', $normalizer->normalizePathInfo('post//123//', '/a')); $this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/')); $this->assertEquals('post/123', $normalizer->normalizePathInfo('post//123//', '')); $normalizer->collapseSlashes = false; $this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/a/')); $this->assertEquals('post//123', $normalizer->normalizePathInfo('post//123//', '/a')); $this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/')); $this->assertEquals('post//123', $normalizer->normalizePathInfo('post//123//', '')); $normalizer->normalizeTrailingSlash = false; $this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/a/')); $this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/a')); $this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/')); $this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '')); $normalizer->collapseSlashes = true; $this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/a/')); $this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/a')); $this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/')); $this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '')); } public function testNormalizeRoute() { $normalizer = new UrlNormalizer(); $route = ['site/index', ['id' => 1, 'name' => 'test']]; // 404 error as default action $normalizer->action = UrlNormalizer::ACTION_NOT_FOUND; $expected = new NotFoundHttpException(Yii::t('yii', 'Page not found.')); try { $result = $normalizer->normalizeRoute($route); $this->fail('Expected throwing NotFoundHttpException'); } catch (NotFoundHttpException $exc) { $this->assertEquals($expected, $exc); } // 301 redirect as default action $normalizer->action = UrlNormalizer::ACTION_REDIRECT_PERMANENT; $expected = new UrlNormalizerRedirectException([$route[0]] + $route[1], 301); try { $result = $normalizer->normalizeRoute($route); $this->fail('Expected throwing UrlNormalizerRedirectException'); } catch (UrlNormalizerRedirectException $exc) { $this->assertEquals($expected, $exc); } // 302 redirect as default action $normalizer->action = UrlNormalizer::ACTION_REDIRECT_TEMPORARY; $expected = new UrlNormalizerRedirectException([$route[0]] + $route[1], 302); try { $result = $normalizer->normalizeRoute($route); $this->fail('Expected throwing UrlNormalizerRedirectException'); } catch (UrlNormalizerRedirectException $exc) { $this->assertEquals($expected, $exc); } // no action $normalizer->action = null; $this->assertEquals($route, $normalizer->normalizeRoute($route)); // custom callback which modifies the route $normalizer->action = function ($route, $normalizer) { $route[0] = 'site/redirect'; $route['normalizeTrailingSlash'] = $normalizer->normalizeTrailingSlash; return $route; }; $expected = $route; $expected[0] = 'site/redirect'; $expected['normalizeTrailingSlash'] = $normalizer->normalizeTrailingSlash; $this->assertEquals($expected, $normalizer->normalizeRoute($route)); // custom callback which throw custom 404 error $normalizer->action = function ($route, $normalizer) { throw new NotFoundHttpException('Custom error message.'); }; $expected = new NotFoundHttpException('Custom error message.'); try { $result = $normalizer->normalizeRoute($route); $this->fail('Expected throwing NotFoundHttpException'); } catch (NotFoundHttpException $exc) { $this->assertEquals($expected, $exc); } } }