ソースを参照

Add more tests

euromark 11 年 前
コミット
961820f4cd

+ 0 - 58
Controller/MyController.php

@@ -36,35 +36,6 @@ class MyController extends Controller {
 	}
 
 	/**
-	 * Fix encoding issues on Apache with mod_rewrite
-	 * Uses Configure::read('App.additionalEncoding') to additionally escape
-	 *
-	 * Tip: Set it to `1` for normal mod_rewrite websites routing directly into webroot
-	 * If you use another setup (like localhost/app/webroot) where you use multiple htaccess files or rewrite
-	 * rules you need to raise it accordingly.
-	 *
-	 * @overwrite to fix encoding issues on Apache with mod_rewrite
-	 * @param string|array $url A string or array-based URL
-	 * @param integer $status Optional HTTP status code (eg: 404)
-	 * @param boolean $exit If true, exit() will be called after the redirect
-	 * @return void
-	 */
-	public function redirect($url, $status = null, $exit = true) {
-		$run = Configure::read('App.additionalEncoding');
-		if ($run && is_array($url)) {
-			foreach ($url as $key => $value) {
-				if ($key === '?') {
-					continue;
-				}
-				$value = $this->_encodeUrlPiece($value, $run);
-
-				$url[$key] = $value;
-			}
-		}
-		return parent::redirect($url, $status, $exit);
-	}
-
-	/**
 	 * Handles automatic pagination of model records.
 	 *
 	 * @overwrite to support defaults like limit, querystring settings
@@ -80,33 +51,4 @@ class MyController extends Controller {
 		return parent::paginate($object, $scope, $whitelist);
 	}
 
-	/**
-	 * Additionally encode string to match the htaccess files processing it.
-	 *
-	 * @param mixed Url piece
-	 * @param integer $run How many times does the value have to be escaped
-	 * @return mixed Escaped piece
-	 */
-	protected function _encodeUrlPiece($value, $run) {
-		if (!is_array($value)) {
-			for ($i = 0; $i < $run; $i++) {
-				$value = urlencode($value);
-			}
-			return $value;
-		}
-		return $this->_encodeUrlPiece($value, $run);
-	}
-
-	/**
-	 * Init Packages class if enabled/included
-	 *
-	 * @deprecated?
-	 */
-	public function beforeRender() {
-		if (class_exists('Packages')) {
-			Packages::initialize($this, __CLASS__);
-		}
-		parent::beforeRender();
-	}
-
 }

+ 36 - 18
Controller/TinyUrlsController.php

@@ -1,21 +1,21 @@
 <?php
 App::uses('ToolsAppController', 'Tools.Controller');
-/*
-
-Apply this route (/Config/routes.php):
-
-Router::connect('/s/:id',
-	array('plugin'=>'tools', 'controller'=>'tiny_urls', 'action'=>'go'),
-	array('id'=>'[0-9a-zA-Z]+'));
-
-Result:
-/domain/s/ID
-
-*/
 
+/**
+ * Tiny Url Generation
+ *
+ * Tip:
+ * Apply this route (/Config/routes.php):
+ *
+ * Router::connect('/s/:id',
+ *   array('plugin' => 'tools', 'controller' => 'tiny_urls', 'action' => 'go'),
+ *   array('id' => '[0-9a-zA-Z]+'));
+ * Result:
+ * /domain/s/ID
+ */
 class TinyUrlsController extends ToolsAppController {
 
-	//public $uses = array('Tools.TinyUrl');
+	public $uses = array('Tools.TinyUrl');
 
 	public function beforeFilter() {
 		parent::beforeFilter();
@@ -31,17 +31,22 @@ class TinyUrlsController extends ToolsAppController {
 
 	/**
 	 * Main redirect function
+	 *
+	 * @return void
 	 */
 	public function go() {
-		if (empty($this->request->params['id'])) {
+		$id = $this->request->query('id');
+		if (!empty($this->request->params['id'])) {
+			$id = $this->request->params['id'];
+		}
+		if (!$id) {
 			throw new NotFoundException();
 		}
-		$entry = $this->TinyUrl->translate($this->request->params['id']);
+		$entry = $this->TinyUrl->translate($id);
 		if (empty($entry)) {
 			throw new NotFoundException();
 		}
 
-		//$message = $entry['TinyInt']['flash_message'];
 		$url = $entry['TinyUrl']['target'];
 
 		if (!empty($message)) {
@@ -52,9 +57,12 @@ class TinyUrlsController extends ToolsAppController {
 		return $this->redirect($url, 301);
 	}
 
+	/**
+	 * TinyUrlsController::admin_index()
+	 *
+	 * @return void
+	 */
 	public function admin_index() {
-		//TODO
-
 		if ($this->Common->isPosted()) {
 			$this->TinyUrl->set($this->request->data);
 			if ($this->TinyUrl->validates()) {
@@ -71,9 +79,19 @@ class TinyUrlsController extends ToolsAppController {
 		$this->set(compact('tinyUrls'));
 	}
 
+	/**
+	 * TinyUrlsController::admin_listing()
+	 *
+	 * @return void
+	 */
 	public function admin_listing() {
 	}
 
+	/**
+	 * TinyUrlsController::admin_reset()
+	 *
+	 * @return void
+	 */
 	public function admin_reset() {
 		if (!$this->Common->isPosted()) {
 			throw new MethodNotAllowedException();

+ 14 - 4
Lib/CurrencyLib.php

@@ -145,8 +145,8 @@ class CurrencyLib {
 			return $historyList;
 		}
 
-		$Xml = Xml::build(self::URL_HISTORY);
-		$currencies = Xml::toArray($Xml);
+		$currencies = $this->_loadXml(self::URL_HISTORY);
+
 		//Filter down to just the rates
 		$currencies = $currencies['Envelope']['Cube']['Cube']['Cube'];
 
@@ -173,8 +173,7 @@ class CurrencyLib {
 		}
 
 		// Retrieve rates as an XML object
-		$CurrencyXml = Xml::build(self::URL);
-		$currencies = Xml::toArray($CurrencyXml);
+		$currencies = $this->_loadXml(self::URL);
 
 		//Filter down to just the rates
 		$currencies = $currencies['Envelope']['Cube']['Cube']['Cube'];
@@ -223,6 +222,17 @@ class CurrencyLib {
 		return false;
 	}
 
+	/**
+	 * CurrencyLib::_loadXml()
+	 *
+	 * @param string $url
+	 * @return array
+	 */
+	protected function _loadXml($url) {
+		$CurrencyXml = Xml::build($url);
+		return Xml::toArray($CurrencyXml);
+	}
+
 	public $currencies = array(
 		'AFA' => 'Afghanistan Afghani',
 		'ALL' => 'Albanian Lek',

+ 6 - 1
Test/Case/Controller/QloginControllerTest.php

@@ -23,6 +23,12 @@ class QloginControllerTest extends ControllerTestCase {
 		$this->QloginController->Auth = $Auth;
 	}
 
+	public function tearDown() {
+		CakeSession::delete('Auth.User');
+
+		parent::tearDown();
+	}
+
 	/**
 	 * QloginControllerTest::testObject()
 	 *
@@ -92,7 +98,6 @@ class QloginControllerTest extends ControllerTestCase {
 	 * @return void
 	 */
 	public function testAdminReset() {
-		$_SERVER['HTTP_REFERER'] = Router::url('/foo/bar', true);
 		$user = array(
 			'id' => 1,
 			'role_id' => 1

+ 106 - 0
Test/Case/Controller/TinyUrlsControllerTest.php

@@ -0,0 +1,106 @@
+<?php
+
+App::uses('TinyUrlsController', 'Tools.Controller');
+
+class TinyUrlsControllerTest extends ControllerTestCase {
+
+	public $fixtures = array('core.cake_session', 'plugin.tools.user', 'plugin.tools.role', 'plugin.tools.tiny_url');
+
+	public $TinyUrlsController;
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->TinyUrlsController = new TestTinyUrlsController(new CakeRequest, new CakeResponse);
+	}
+
+	public function tearDown() {
+		CakeSession::delete('Auth.User');
+
+		parent::tearDown();
+	}
+
+	/**
+	 * TinyUrlsControllerTest::testObject()
+	 *
+	 * @return void
+	 */
+	public function testObject() {
+		$this->assertTrue(is_object($this->TinyUrlsController));
+		$this->assertInstanceOf('TinyUrlsController', $this->TinyUrlsController);
+	}
+
+	/**
+	 * QloginControllerTest::testGo()
+	 *
+	 * @return void
+	 */
+	public function testGo() {
+		$this->TinyUrl = ClassRegistry::init('Tools.TinyUrl');
+
+		$url = $this->TinyUrl->url(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
+		$this->assertContains('/tools/tiny_urls/go?id=m', $url);
+
+		$key = 'm';
+		$this->TinyUrlsController->request['id'] = 'm';
+		$this->TinyUrlsController->go();
+		$this->assertTextContains('/test/foo/bar', $this->TinyUrlsController->redirectUrl);
+
+		// Invalid id
+		$this->expectException('NotFoundException');
+		$key = 'm';
+		$this->TinyUrlsController->request['id'] = 's';
+		$this->TinyUrlsController->go();
+	}
+
+	/**
+	 * QloginControllerTest::testAdminIndex()
+	 *
+	 * @return void
+	 */
+	public function testAdminIndex() {
+		$user = array(
+			'id' => 1,
+			'role_id' => 1
+		);
+		CakeSession::write('Auth.User', $user);
+
+		$url = Router::url(array('admin' => true, 'plugin' => 'tools', 'controller' => 'tiny_urls', 'action' => 'index'));
+		$result = $this->testAction($url, array(
+			'method' => 'get',
+			'return' => 'contents'
+		));
+		$this->assertNotEmpty($result);
+	}
+
+	/**
+	 * QloginControllerTest::testAdminIndex()
+	 *
+	 * @return void
+	 */
+	public function testAdminReset() {
+		$user = array(
+			'id' => 1,
+			'role_id' => 1
+		);
+		CakeSession::write('Auth.User', $user);
+
+		$url = Router::url(array('admin' => true, 'plugin' => 'tools', 'controller' => 'tiny_urls', 'action' => 'reset'));
+		$result = $this->testAction($url, array(
+			'return' => 'contents'
+		));
+		$this->assertNull($result);
+		$this->assertTextContains('admin/tools/tiny_urls', $this->headers['Location']);
+	}
+
+}
+
+class TestTinyUrlsController extends TinyUrlsController {
+
+	public $redirectUrl	 = null;
+
+	public function redirect($url, $status = null, $exit = true) {
+		$this->redirectUrl = $url;
+	}
+
+}

+ 39 - 2
Test/Case/Lib/CurrencyLibTest.php

@@ -7,15 +7,22 @@ class CurrencyLibTest extends MyCakeTestCase {
 	public function setUp() {
 		parent::setUp();
 
-		$this->CurrencyLib = new CurrencyLib();
+		$this->CurrencyLib = new TestCurrencyLib();
 	}
 
+	/**
+	 * CurrencyLibTest::testStartReset()
+	 *
+	 * @return void
+	 */
 	public function testStartReset() {
 		$this->CurrencyLib->reset();
 	}
 
 	/**
-	 * test
+	 * CurrencyLibTest::testConvert()
+	 *
+	 * @return void
 	 */
 	public function testConvert() {
 		$this->out('<h2>30 EUR in USD</h2>', true);
@@ -26,6 +33,11 @@ class CurrencyLibTest extends MyCakeTestCase {
 		$this->assertFalse($this->CurrencyLib->cacheFileUsed());
 	}
 
+	/**
+	 * CurrencyLibTest::testIsAvailable()
+	 *
+	 * @return void
+	 */
 	public function testIsAvailable() {
 		$is = $this->CurrencyLib->isAvailable('EUR');
 		$this->assertTrue($is);
@@ -34,6 +46,11 @@ class CurrencyLibTest extends MyCakeTestCase {
 		$this->assertFalse($is);
 	}
 
+	/**
+	 * CurrencyLibTest::testTable()
+	 *
+	 * @return void
+	 */
 	public function testTable() {
 		$this->out('<h2>Currency Table</h2>', true);
 		$is = $this->CurrencyLib->table();
@@ -46,9 +63,29 @@ class CurrencyLibTest extends MyCakeTestCase {
 		$this->assertTrue($this->CurrencyLib->cacheFileUsed());
 	}
 
+	/**
+	 * CurrencyLibTest::testReset()
+	 *
+	 * @return void
+	 */
 	public function testReset() {
 		$res = $this->CurrencyLib->reset();
 		$this->assertTrue($res === null || $res === true);
 	}
 
+}
+
+class TestCurrencyLib extends CurrencyLib {
+
+	protected function _loadXml($url) {
+		if (php_sapi_name() !== 'cli' && !empty($_GET) && !empty($_GET['debug'])) {
+			debug('Live Data!');
+			return parent::_loadXml($url);
+		}
+
+		$file = basename($url);
+		$url = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'xml' . DS . $file;
+		return parent::_loadXml($url);
+	}
+
 }

+ 43 - 0
Test/test_files/xml/eurofxref-daily.xml

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
+	<gesmes:subject>Reference rates</gesmes:subject>
+	<gesmes:Sender>
+		<gesmes:name>European Central Bank</gesmes:name>
+	</gesmes:Sender>
+	<Cube>
+		<Cube time='2014-03-19'>
+			<Cube currency='USD' rate='1.3913'/>
+			<Cube currency='JPY' rate='141.31'/>
+			<Cube currency='BGN' rate='1.9558'/>
+			<Cube currency='CZK' rate='27.460'/>
+			<Cube currency='DKK' rate='7.4641'/>
+			<Cube currency='GBP' rate='0.83680'/>
+			<Cube currency='HUF' rate='310.08'/>
+			<Cube currency='LTL' rate='3.4528'/>
+			<Cube currency='PLN' rate='4.1982'/>
+			<Cube currency='RON' rate='4.4915'/>
+			<Cube currency='SEK' rate='8.8339'/>
+			<Cube currency='CHF' rate='1.2167'/>
+			<Cube currency='NOK' rate='8.3175'/>
+			<Cube currency='HRK' rate='7.6598'/>
+			<Cube currency='RUB' rate='49.9800'/>
+			<Cube currency='TRY' rate='3.0910'/>
+			<Cube currency='AUD' rate='1.5269'/>
+			<Cube currency='BRL' rate='3.2493'/>
+			<Cube currency='CAD' rate='1.5545'/>
+			<Cube currency='CNY' rate='8.6212'/>
+			<Cube currency='HKD' rate='10.8026'/>
+			<Cube currency='IDR' rate='15740.61'/>
+			<Cube currency='ILS' rate='4.8167'/>
+			<Cube currency='INR' rate='84.7928'/>
+			<Cube currency='KRW' rate='1488.77'/>
+			<Cube currency='MXN' rate='18.3039'/>
+			<Cube currency='MYR' rate='4.5667'/>
+			<Cube currency='NZD' rate='1.6142'/>
+			<Cube currency='PHP' rate='62.404'/>
+			<Cube currency='SGD' rate='1.7615'/>
+			<Cube currency='THB' rate='44.754'/>
+			<Cube currency='ZAR' rate='14.9134'/>
+		</Cube>
+	</Cube>
+</gesmes:Envelope>

ファイルの差分が大きいため隠しています
+ 2 - 0
Test/test_files/xml/eurofxref-hist.xml