From df0f2295c3d9fabba6c5bcbdf46e93be6e398def Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 2 Jun 2015 23:06:06 -0400 Subject: [PATCH 1/4] Fix issue with overlapping irregular inflections. When irregular inflections overlap we should choose the longest match, not the shortest. Refs #6659 --- lib/Cake/Test/Case/Utility/InflectorTest.php | 46 ++++++++++++++++++++ lib/Cake/Utility/Inflector.php | 12 +++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/InflectorTest.php b/lib/Cake/Test/Case/Utility/InflectorTest.php index c1abaf15a..8635f39f4 100644 --- a/lib/Cake/Test/Case/Utility/InflectorTest.php +++ b/lib/Cake/Test/Case/Utility/InflectorTest.php @@ -183,6 +183,29 @@ class InflectorTest extends CakeTestCase { $this->assertEquals(Inflector::singularize(''), ''); } +/** + * Test that overlapping irregulars don't collide. + * + * @return void + */ + public function testSingularizeMultiWordIrregular() { + Inflector::rules('singular', array( + 'irregular' => array( + 'preguntas_frecuentes' => 'pregunta_frecuente', + 'categorias_preguntas_frecuentes' => 'categoria_pregunta_frecuente', + ) + )); + $this->assertEquals('pregunta_frecuente', Inflector::singularize('preguntas_frecuentes')); + $this->assertEquals( + 'categoria_pregunta_frecuente', + Inflector::singularize('categorias_preguntas_frecuentes') + ); + $this->assertEquals( + 'faq_categoria_pregunta_frecuente', + Inflector::singularize('faq_categorias_preguntas_frecuentes') + ); + } + /** * testInflectingPlurals method * @@ -256,6 +279,29 @@ class InflectorTest extends CakeTestCase { $this->assertEquals(Inflector::pluralize(''), ''); } +/** + * Test that overlapping irregulars don't collide. + * + * @return void + */ + public function testPluralizeMultiWordIrregular() { + Inflector::rules('plural', array( + 'irregular' => array( + 'pregunta_frecuente' => 'preguntas_frecuentes', + 'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes', + ) + )); + $this->assertEquals('preguntas_frecuentes', Inflector::pluralize('pregunta_frecuente')); + $this->assertEquals( + 'categorias_preguntas_frecuentes', + Inflector::pluralize('categoria_pregunta_frecuente') + ); + $this->assertEquals( + 'faq_categorias_preguntas_frecuentes', + Inflector::pluralize('faq_categoria_pregunta_frecuente') + ); + } + /** * testInflectingMultiWordIrregulars * diff --git a/lib/Cake/Utility/Inflector.php b/lib/Cake/Utility/Inflector.php index 61a63c2a1..d06d4baff 100644 --- a/lib/Cake/Utility/Inflector.php +++ b/lib/Cake/Utility/Inflector.php @@ -386,8 +386,10 @@ class Inflector { self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')'; } - if (preg_match('/(.*(?:\\b|_))(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) { - self::$_cache['pluralize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1); + if (preg_match('/(.*?(?:\\b|_))(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) { + self::$_cache['pluralize'][$word] = $regs[1] . + substr($regs[2], 0, 1) . + substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1); return self::$_cache['pluralize'][$word]; } @@ -435,8 +437,10 @@ class Inflector { self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')'; } - if (preg_match('/(.*(?:\\b|_))(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) { - self::$_cache['singularize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1); + if (preg_match('/(.*?(?:\\b|_))(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) { + self::$_cache['singularize'][$word] = $regs[1] . + substr($regs[2], 0, 1) . + substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1); return self::$_cache['singularize'][$word]; } From 239c83938f1c238900fef1ab0996a6cfd9975727 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 5 Jun 2015 10:20:51 -0400 Subject: [PATCH 2/4] Fix regression in camelize(). The input should not be lowercased before camelizing, as this can cause inputs that were previously camelized to create incorrect results. Refs #6735 --- lib/Cake/Test/Case/Utility/InflectorTest.php | 14 ++++++++++++++ lib/Cake/Utility/Inflector.php | 1 - 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Utility/InflectorTest.php b/lib/Cake/Test/Case/Utility/InflectorTest.php index 8635f39f4..9f54ee1fc 100644 --- a/lib/Cake/Test/Case/Utility/InflectorTest.php +++ b/lib/Cake/Test/Case/Utility/InflectorTest.php @@ -460,6 +460,20 @@ class InflectorTest extends CakeTestCase { $this->assertSame(Inflector::underscore(false), ''); } + +/** + * Test camelize() + * + * @return void + */ + public function testCamelize() { + $this->assertSame('BlogArticles', Inflector::camelize('blog_articles')); + $this->assertSame('BlogArticles', Inflector::camelize('blog articles')); + $this->assertSame('MyPlugin.MyClass', Inflector::camelize('MyPlugin.MyClass')); + $this->assertSame('MyPlugin.MyClass', Inflector::camelize('my_plugin.MyClass')); + $this->assertSame('MyPlugin.myClass', Inflector::camelize('MyPlugin.my_class')); + } + /** * testVariableNaming method * diff --git a/lib/Cake/Utility/Inflector.php b/lib/Cake/Utility/Inflector.php index d06d4baff..6863d95bb 100644 --- a/lib/Cake/Utility/Inflector.php +++ b/lib/Cake/Utility/Inflector.php @@ -500,7 +500,6 @@ class Inflector { */ public static function humanize($lowerCaseAndUnderscoredWord) { if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) { - $lowerCaseAndUnderscoredWord = self::underscore($lowerCaseAndUnderscoredWord); $result = explode(' ', str_replace('_', ' ', $lowerCaseAndUnderscoredWord)); foreach ($result as &$word) { $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1); From 0e6fcc02b809b5885009e458a04c67e3c9427b57 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 5 Jun 2015 10:31:29 -0400 Subject: [PATCH 3/4] Add/correct some tests for humanize. The arguments for assertEquals() were backwards. While there are many more flipped assertions I will change the others separately. --- lib/Cake/Test/Case/Utility/InflectorTest.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/InflectorTest.php b/lib/Cake/Test/Case/Utility/InflectorTest.php index 9f54ee1fc..e3fd21191 100644 --- a/lib/Cake/Test/Case/Utility/InflectorTest.php +++ b/lib/Cake/Test/Case/Utility/InflectorTest.php @@ -504,10 +504,10 @@ class InflectorTest extends CakeTestCase { * @return void */ public function testTableNaming() { - $this->assertEquals(Inflector::tableize('ArtistsGenre'), 'artists_genres'); - $this->assertEquals(Inflector::tableize('FileSystem'), 'file_systems'); - $this->assertEquals(Inflector::tableize('News'), 'news'); - $this->assertEquals(Inflector::tableize('Bureau'), 'bureaus'); + $this->assertEquals('artists_genres', Inflector::tableize('ArtistsGenre')); + $this->assertEquals('file_systems', Inflector::tableize('FileSystem')); + $this->assertEquals('news', Inflector::tableize('News')); + $this->assertEquals('bureaus', Inflector::tableize('Bureau')); } /** @@ -516,11 +516,12 @@ class InflectorTest extends CakeTestCase { * @return void */ public function testHumanization() { - $this->assertEquals(Inflector::humanize('posts'), 'Posts'); - $this->assertEquals(Inflector::humanize('posts_tags'), 'Posts Tags'); - $this->assertEquals(Inflector::humanize('file_systems'), 'File Systems'); - $this->assertEquals(Inflector::humanize('hello_wörld'), 'Hello Wörld'); - $this->assertEquals(Inflector::humanize('福岡_city'), '福岡 City'); + $this->assertEquals('Posts', Inflector::humanize('posts')); + $this->assertEquals('Posts Tags', Inflector::humanize('posts_tags')); + $this->assertEquals('File Systems', Inflector::humanize('file_systems')); + $this->assertEquals('FiLe SysTems', Inflector::humanize('FiLe_SysTems')); + $this->assertEquals('Hello Wörld', Inflector::humanize('hello_wörld')); + $this->assertEquals('福岡 City', Inflector::humanize('福岡_city')); } /** From 6d60e6a4db395d7698cd748b3255fb525627ae30 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 7 Jun 2015 15:45:16 -0400 Subject: [PATCH 4/4] Backport 7eec48268ebb6a17656df4a059f9e7b43991472f to 2.x Backport fixes to base path generation that prevent issue when a URL contains // it can circumvent the base path generation, which results in unwanted user data in the base/webroot paths. This creates an opportunity for CSS manipulation in old versions of IE, and newer ones via iframe inheritance. --- lib/Cake/Network/CakeRequest.php | 2 ++ lib/Cake/Test/Case/Network/CakeRequestTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index a51a74c65..eb9f9905f 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -293,6 +293,8 @@ class CakeRequest implements ArrayAccess { if (!$baseUrl) { $base = dirname(env('PHP_SELF')); + // Clean up additional / which cause following code to fail.. + $base = preg_replace('#/+#', '/', $base); $indexPos = strpos($base, '/webroot/index.php'); if ($indexPos !== false) { diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index 88bbbba6a..accf7c6c6 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -1361,6 +1361,24 @@ class CakeRequestTest extends CakeTestCase { $this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here); } + /** + * Test that even if mod_rewrite is on, and the url contains index.php + * and there are numerous //s that the base/webroot is calculated correctly. + * + * @return void + */ + public function testBaseUrlWithModRewriteAndExtraSlashes() { + $_SERVER['REQUEST_URI'] = '/cakephp/webroot///index.php/bananas/eat'; + $_SERVER['PHP_SELF'] = '/cakephp/webroot///index.php/bananas/eat'; + $_SERVER['PATH_INFO'] = '/bananas/eat'; + $request = new CakeRequest(); + + $this->assertEquals('/cakephp', $request->base); + $this->assertEquals('/cakephp/', $request->webroot); + $this->assertEquals('bananas/eat', $request->url); + $this->assertEquals('/cakephp/bananas/eat', $request->here); + } + /** * Test base, webroot, and URL parsing when there is no URL rewriting *