From 91838d6d5d6f2a5b07a6b8fa41de2fc0a41b7656 Mon Sep 17 00:00:00 2001 From: nate Date: Fri, 19 Sep 2008 19:48:22 +0000 Subject: [PATCH] Fixing bug in RequestHandler::prefers() not respecting accept order for single items, test case updated git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7634 3807eeeb-6ff5-0310-8944-8be069107fe0 --- .../controller/components/request_handler.php | 26 ++++++++++++++----- .../components/request_handler.test.php | 5 +++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index 10b6316b8..2d7761b49 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -500,9 +500,10 @@ class RequestHandlerComponent extends Object { */ function prefers($type = null) { $this->__initializeTypes(); + $accept = $this->accepts(); + if ($type == null) { if (empty($this->ext)) { - $accept = $this->accepts(null); if (is_array($accept)) { return $accept[0]; } @@ -510,20 +511,31 @@ class RequestHandlerComponent extends Object { } return $this->ext; } - App::import('Core', 'Set'); - $types = Set::normalize($type, false); + + if (is_string($type)) { + $types = array($type); + } + + if (count($types) === 1) { + if (!empty($this->ext)) { + return ($types[0] == $this->ext); + } + return ($types[0] == $accept[0]); + } $accepts = array(); foreach ($types as $type) { - if ($this->accepts($type)) { + if (in_array($type, $accept)) { $accepts[] = $type; } } - if (count($accepts) == 0) { + if (count($accepts) === 0) { return false; - } elseif (count($accepts) == 1) { - return $accepts[0]; + } elseif (count($types) === 1) { + return ($types[0] === $accepts[0]); + } elseif (count($accepts) === 1) { + return $accepts[0]; } $accepts = array_intersect($this->__acceptTypes, $accepts); diff --git a/cake/tests/cases/libs/controller/components/request_handler.test.php b/cake/tests/cases/libs/controller/components/request_handler.test.php index 1b5e08c9a..e82a453e1 100644 --- a/cake/tests/cases/libs/controller/components/request_handler.test.php +++ b/cake/tests/cases/libs/controller/components/request_handler.test.php @@ -360,17 +360,20 @@ class RequestHandlerComponentTest extends CakeTestCase { $this->assertNotEqual($this->RequestHandler->prefers(), 'rss'); $this->RequestHandler->ext = 'rss'; $this->assertEqual($this->RequestHandler->prefers(), 'rss'); + $this->assertFalse($this->RequestHandler->prefers('xml')); + $this->assertTrue($this->RequestHandler->accepts('xml')); $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; $this->_init(); $this->assertEqual($this->RequestHandler->prefers(), 'xml'); $this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml'); - $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo'))); $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5'; $this->_init(); $this->assertEqual($this->RequestHandler->prefers(), 'html'); + $this->assertFalse($this->RequestHandler->prefers('rss')); + $this->assertFalse($this->RequestHandler->accepts('rss')); } /** * testCustomContent method