From 323e8d8d76af3d182b4c3ebe2ac3ae26f28d2b84 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Tue, 12 May 2015 14:33:15 +0200 Subject: [PATCH] Add underscore support for multi word irregulars. Underscore separated words were not catched by the irregular regex, tests however didn't fail as the default rules matched the tested words too. The added test should ensure that this won't happen again. Fixes the gap left by the previous #6538 fix. --- lib/Cake/Test/Case/Utility/InflectorTest.php | 25 ++++++++++++++++++++ lib/Cake/Utility/Inflector.php | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/InflectorTest.php b/lib/Cake/Test/Case/Utility/InflectorTest.php index 343215098..b621a1920 100644 --- a/lib/Cake/Test/Case/Utility/InflectorTest.php +++ b/lib/Cake/Test/Case/Utility/InflectorTest.php @@ -256,6 +256,31 @@ class InflectorTest extends CakeTestCase { $this->assertEquals(Inflector::pluralize(''), ''); } +/** + * testInflectingMultiWordIrregulars + * + * @return void + */ + public function testInflectingMultiWordIrregulars() { + // unset the default rules in order to avoid them possibly matching + // the words in case the irregular regex won't match, the tests + // should fail in that case + Inflector::rules('plural', array( + 'rules' => array(), + )); + Inflector::rules('singular', array( + 'rules' => array(), + )); + + $this->assertEquals(Inflector::singularize('wisdom teeth'), 'wisdom tooth'); + $this->assertEquals(Inflector::singularize('wisdom-teeth'), 'wisdom-tooth'); + $this->assertEquals(Inflector::singularize('wisdom_teeth'), 'wisdom_tooth'); + + $this->assertEquals(Inflector::pluralize('sweet potato'), 'sweet potatoes'); + $this->assertEquals(Inflector::pluralize('sweet-potato'), 'sweet-potatoes'); + $this->assertEquals(Inflector::pluralize('sweet_potato'), 'sweet_potatoes'); + } + /** * testInflectorSlug method * diff --git a/lib/Cake/Utility/Inflector.php b/lib/Cake/Utility/Inflector.php index 0d245c517..594556b60 100644 --- a/lib/Cake/Utility/Inflector.php +++ b/lib/Cake/Utility/Inflector.php @@ -386,7 +386,7 @@ class Inflector { self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')'; } - if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) { + 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,7 +435,7 @@ class Inflector { self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')'; } - if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) { + 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]; }