浏览代码

Add tests

euromark 11 年之前
父节点
当前提交
f8c2b2dda6
共有 2 个文件被更改,包括 176 次插入0 次删除
  1. 170 0
      Test/Case/View/Helper/MyHelperUrlCacheTest.php
  2. 6 0
      View/Helper/MyHelper.php

+ 170 - 0
Test/Case/View/Helper/MyHelperUrlCacheTest.php

@@ -0,0 +1,170 @@
+<?php
+
+App::uses('View', 'View');
+App::uses('Controller', 'Controller');
+App::uses('Cache', 'Cache');
+App::uses('MyHelper', 'Tools.View/Helper');
+App::uses('CakeRequest', 'Network');
+
+class MyHelperUrlCacheTest extends CakeTestCase {
+
+	public $HtmlHelper = null;
+
+	public function setUp() {
+		parent::setUp();
+
+		Configure::write('UrlCache.active', true);
+		Configure::write('UrlCache.pageFiles', true);
+		Configure::write('UrlCache.verbosePrefixes', true);
+		Configure::write('Routing.prefixes', array('admin'));
+
+		$this->HtmlHelper = new MyHelper(new View(new Controller(new CakeRequest('/test', false))));
+		$this->HtmlHelper->beforeRender('foo');
+	}
+
+	public function tearDown() {
+		Cache::delete(UrlCacheManager::$cacheKey, '_cake_core_');
+		Cache::delete(UrlCacheManager::$cachePageKey, '_cake_core_');
+		Configure::delete('UrlCache');
+
+		parent::tearDown();
+	}
+
+	public function testInstance() {
+		$this->assertTrue(is_a($this->HtmlHelper, 'MyHelper'));
+	}
+
+	public function testSettings() {
+		$settings = Configure::read('UrlCache');
+		$this->assertTrue($settings['active']);
+	}
+
+	public function testUrlRelative() {
+		$url = $this->HtmlHelper->url(array('controller' => 'posts'));
+		$this->assertEqual($url, '/posts');
+		$this->assertEqual(array('779e416667ffd33e75ac19e0952abb47' => '/posts'), UrlCacheManager::$cache);
+
+		$this->HtmlHelper->afterLayout('foo');
+		$cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
+		$this->assertEqual(array('779e416667ffd33e75ac19e0952abb47' => '/posts'), $cache);
+	}
+
+	public function testUrlFull() {
+		$url = $this->HtmlHelper->url(array('controller' => 'posts'), true);
+		$this->assertPattern('/http:\/\/(.*)\/posts/', $url);
+		$this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
+		$this->assertPattern('/http:\/\/(.*)\/posts/', UrlCacheManager::$cache['8f45f5c31d138d700742b01ccb673e1e']);
+
+		$this->HtmlHelper->afterLayout('foo');
+		$cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
+		$this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
+		$this->assertPattern('/http:\/\/(.*)\/posts/', $cache['8f45f5c31d138d700742b01ccb673e1e']);
+	}
+
+	public function testUrlRelativeAndFull() {
+		$this->HtmlHelper->url(array('controller' => 'posts'));
+		$this->HtmlHelper->url(array('controller' => 'posts'), true);
+
+		$this->assertEqual(array('779e416667ffd33e75ac19e0952abb47', '8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
+
+		$this->HtmlHelper->afterLayout('foo');
+		$cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
+		$this->assertEqual(array('779e416667ffd33e75ac19e0952abb47', '8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
+	}
+
+	public function testUrlWithParams() {
+		$this->HtmlHelper->url(array('controller' => 'posts'), true);
+		$this->HtmlHelper->url(array('controller' => 'posts', 'action' => 'view', '3'));
+
+		$this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
+		$this->assertEqual(array('e2ff5470228f80f98b2be7ddbcab340d'), array_keys(UrlCacheManager::$cachePage));
+
+		$this->HtmlHelper->afterLayout('foo');
+		$cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
+		$this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
+		$cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
+		$this->assertEqual(array('e2ff5470228f80f98b2be7ddbcab340d'), array_keys($cache));
+	}
+
+	public function testUrlWithDifferentOrders() {
+		$this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view'));
+		$this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools'));
+		$this->assertEqual(array('0ba1e9d8ab27a564450be9aa93bd4a1c'), array_keys(UrlCacheManager::$cache));
+
+		$this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', '3'));
+		$this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', '3'));
+		$this->assertEqual(array('f5509ea5e31562b541948d89b4d65700'), array_keys(UrlCacheManager::$cachePage));
+
+		$this->HtmlHelper->afterLayout('foo');
+		$cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
+		$this->assertEqual(array('0ba1e9d8ab27a564450be9aa93bd4a1c'), array_keys($cache));
+		$cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
+		$this->assertEqual(array('f5509ea5e31562b541948d89b4d65700'), array_keys($cache));
+	}
+
+	public function testUrlWithDifferentValues() {
+		$url = $this->HtmlHelper->url(array('plugin' => false, 'controller' => 'posts', 'action' => 'details', 1));
+		$url2 = $this->HtmlHelper->url(array('plugin' => null, 'controller' => 'posts', 'action' => 'details', '1'));
+		$url3 = $this->HtmlHelper->url(array('plugin' => '', 'controller' => 'posts', 'action' => 'details', true));
+		$this->assertEqual($url, $url2);
+		$this->assertEqual($url2, $url3);
+		$this->assertEqual(array('f0fdde123fe5958781cfad4284ee59c4'), array_keys(UrlCacheManager::$cachePage));
+
+		$this->HtmlHelper->afterLayout('foo');
+		$cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
+		$this->assertEqual(array('f0fdde123fe5958781cfad4284ee59c4'), array_keys($cache));
+	}
+
+	public function testGlobalCache() {
+		$res = UrlCacheManager::get(array('controller' => 'posts', 'action' => 'index'), false);
+		$this->assertEqual(false, $res);
+
+		$url = $this->HtmlHelper->url(array('controller' => 'posts', 'action' => 'index'));
+		$this->HtmlHelper->afterLayout('foo');
+
+		# on second page the same url will not trigger a miss but a hit
+		$this->HtmlHelper->beforeRender('foo');
+		$res = UrlCacheManager::get(array('controller' => 'posts', 'action' => 'index'), false);
+		$this->assertEqual($url, $res);
+
+		$this->HtmlHelper->afterLayout('foo');
+	}
+
+	public function testUrlWithVerbosePrefixes() {
+		$url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'admin' => true));
+		$url2 = $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'admin' => true));
+		$this->assertEqual(array('3016e3546edf1c2152905da5a42ea13f'), array_keys(UrlCacheManager::$cache));
+		$this->assertEqual('/admin/tools/posts/view', $url);
+		$this->assertEqual($url, $url2);
+
+		$this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'admin' => true, 'some' => 'param'));
+		$this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'some' => 'param', 'admin' => true));
+		$this->assertEqual(array('25a924190d6e0ed09127d1904a286a95'), array_keys(UrlCacheManager::$cachePage));
+
+		$this->HtmlHelper->afterLayout('foo');
+	}
+
+	public function testUrlWithoutVerbosePrefixes() {
+		$this->skipIf(true, 'doesnt work yet');
+
+		Configure::delete('UrlCache');
+		Configure::write('UrlCache.active', true);
+		Configure::write('UrlCache.pageFiles', true);
+		Configure::write('UrlCache.verbosePrefixes', false);
+		UrlCacheManager::$paramFields = array('controller', 'plugin', 'action', 'prefix');
+		$this->HtmlHelper->beforeRender('foo');
+
+		$url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'prefix' => 'admin'));
+		$this->assertEqual(array('41d5d7eb9442adbe76e6c7ebbb02ecc7'), array_keys(UrlCacheManager::$cache));
+		$this->assertEqual('/admin/tools/posts/view', $url);
+
+		$url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'prefix' => 'admin', 'some' => 'param'));
+		$this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'some' => 'param', 'prefix' => 'admin'));
+		$this->assertEqual('/admin/tools/posts/view/some:param', $url);
+		$this->assertEqual(array('6a7091b88c8132ebb5461851808d318a'), array_keys(UrlCacheManager::$cachePage));
+
+		$this->HtmlHelper->afterLayout('foo');
+	}
+
+}
+

+ 6 - 0
View/Helper/MyHelper.php

@@ -51,6 +51,9 @@ class MyHelper extends Helper {
 		if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.beforeRender')) {
 			return;
 		}
+		if (empty($this->request)) {
+			return;
+		}
 
 		// todo: maybe lazy load with HtmlHelper::url()?
 		UrlCacheManager::init($this->_View);
@@ -66,6 +69,9 @@ class MyHelper extends Helper {
 		if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
 			return;
 		}
+		if (empty($this->request)) {
+			return;
+		}
 
 		UrlCacheManager::finalize();
 		Configure::write('UrlCache.runtime.afterLayout', true);