From 10758eecd1d0c5a3892322192433310487071d6c Mon Sep 17 00:00:00 2001 From: Tyler Cosgrove Date: Wed, 17 Feb 2016 18:11:46 -0500 Subject: [PATCH 1/8] Add InflectorTest --- tests/unit/Grav/Common/InflectorTest.php | 127 +++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 tests/unit/Grav/Common/InflectorTest.php diff --git a/tests/unit/Grav/Common/InflectorTest.php b/tests/unit/Grav/Common/InflectorTest.php new file mode 100644 index 000000000..d8a1fc4ff --- /dev/null +++ b/tests/unit/Grav/Common/InflectorTest.php @@ -0,0 +1,127 @@ +grav = Fixtures::get('grav'); + $this->inflector = $this->grav['inflector']; + } + + protected function _after() + { + } + + public function testPluralize() + { + $this->assertSame('words', $this->inflector->pluralize('word')); + $this->assertSame('kisses', $this->inflector->pluralize('kiss')); + $this->assertSame('volcanoes', $this->inflector->pluralize('volcanoe')); + $this->assertSame('cherries', $this->inflector->pluralize('cherry')); + $this->assertSame('days', $this->inflector->pluralize('day')); + $this->assertSame('knives', $this->inflector->pluralize('knife')); + } + + public function testSingularize() + { + $this->assertSame('word', $this->inflector->singularize('words')); + $this->assertSame('kiss', $this->inflector->singularize('kisses')); + $this->assertSame('volcanoe', $this->inflector->singularize('volcanoe')); + $this->assertSame('cherry', $this->inflector->singularize('cherries')); + $this->assertSame('day', $this->inflector->singularize('days')); + $this->assertSame('knife', $this->inflector->singularize('knives')); + } + + public function testTitleize() + { + $this->assertSame('This String Is Titleized', $this->inflector->titleize('ThisStringIsTitleized')); + $this->assertSame('This String Is Titleized', $this->inflector->titleize('this string is titleized')); + $this->assertSame('This String Is Titleized', $this->inflector->titleize('this_string_is_titleized')); + $this->assertSame('This String Is Titleized', $this->inflector->titleize('this-string-is-titleized')); + + $this->assertSame('This string is titleized', $this->inflector->titleize('ThisStringIsTitleized', 'first')); + $this->assertSame('This string is titleized', $this->inflector->titleize('this string is titleized', 'first')); + $this->assertSame('This string is titleized', $this->inflector->titleize('this_string_is_titleized', 'first')); + $this->assertSame('This string is titleized', $this->inflector->titleize('this-string-is-titleized', 'first')); + } + + public function testCamelize() + { + $this->assertSame('ThisStringIsCamelized', $this->inflector->camelize('This String Is Camelized')); + $this->assertSame('ThisStringIsCamelized', $this->inflector->camelize('thisStringIsCamelized')); + $this->assertSame('ThisStringIsCamelized', $this->inflector->camelize('This_String_Is_Camelized')); + $this->assertSame('ThisStringIsCamelized', $this->inflector->camelize('this string is camelized')); + $this->assertSame('GravSPrettyCoolMy1', $this->inflector->camelize("Grav's Pretty Cool. My #1!")); + } + + public function testUnderscorize() + { + $this->assertSame('this_string_is_underscorized', $this->inflector->underscorize('This String Is Underscorized')); + $this->assertSame('this_string_is_underscorized', $this->inflector->underscorize('ThisStringIsUnderscorized')); + $this->assertSame('this_string_is_underscorized', $this->inflector->underscorize('This_String_Is_Underscorized')); + $this->assertSame('this_string_is_underscorized', $this->inflector->underscorize('This-String-Is-Underscorized')); + } + + public function testHyphenize() + { + $this->assertSame('this-string-is-hyphenized', $this->inflector->hyphenize('This String Is Hyphenized')); + $this->assertSame('this-string-is-hyphenized', $this->inflector->hyphenize('ThisStringIsHyphenized')); + $this->assertSame('this-string-is-hyphenized', $this->inflector->hyphenize('This-String-Is-Hyphenized')); + $this->assertSame('this-string-is-hyphenized', $this->inflector->hyphenize('This_String_Is_Hyphenized')); + } + + public function testHumanize() + { + //$this->assertSame('This string is humanized', $this->inflector->humanize('ThisStringIsHumanized')); + $this->assertSame('This string is humanized', $this->inflector->humanize('this_string_is_humanized')); + //$this->assertSame('This string is humanized', $this->inflector->humanize('this-string-is-humanized')); + + $this->assertSame('This String Is Humanized', $this->inflector->humanize('this_string_is_humanized'), 'all'); + //$this->assertSame('This String Is Humanized', $this->inflector->humanize('this-string-is-humanized'), 'all'); + } + + public function testVariablize() + { + $this->assertSame('thisStringIsVariablized', $this->inflector->variablize('This String Is Variablized')); + $this->assertSame('thisStringIsVariablized', $this->inflector->variablize('ThisStringIsVariablized')); + $this->assertSame('thisStringIsVariablized', $this->inflector->variablize('This_String_Is_Variablized')); + $this->assertSame('thisStringIsVariablized', $this->inflector->variablize('this string is variablized')); + $this->assertSame('gravSPrettyCoolMy1', $this->inflector->variablize("Grav's Pretty Cool. My #1!")); + } + + public function testTableize() + { + + } + + public function testClassify() + { + + } + + public function testOrdinalize() + { + + } + + public function testMonthize() + { + + } +} + + From 1d90107a5a5610250e45fe8714163b7a067dbd56 Mon Sep 17 00:00:00 2001 From: Tyler Cosgrove Date: Wed, 17 Feb 2016 18:27:40 -0500 Subject: [PATCH 2/8] Fix Humanize Test --- tests/unit/Grav/Common/InflectorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Grav/Common/InflectorTest.php b/tests/unit/Grav/Common/InflectorTest.php index d8a1fc4ff..e7c007209 100644 --- a/tests/unit/Grav/Common/InflectorTest.php +++ b/tests/unit/Grav/Common/InflectorTest.php @@ -90,7 +90,7 @@ class InflectorTest extends \Codeception\TestCase\Test $this->assertSame('This string is humanized', $this->inflector->humanize('this_string_is_humanized')); //$this->assertSame('This string is humanized', $this->inflector->humanize('this-string-is-humanized')); - $this->assertSame('This String Is Humanized', $this->inflector->humanize('this_string_is_humanized'), 'all'); + $this->assertSame('This String Is Humanized', $this->inflector->humanize('this_string_is_humanized', 'all')); //$this->assertSame('This String Is Humanized', $this->inflector->humanize('this-string-is-humanized'), 'all'); } From 80f9c5782a6ad9752f9acd70cf084e5b6f61e604 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 19 Feb 2016 10:35:43 -0700 Subject: [PATCH 3/8] Added function to allow you to add variable key/value pairs into an array --- system/src/Grav/Common/Twig/TwigExtension.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index dce86fc62..d2035d800 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -96,6 +96,7 @@ class TwigExtension extends \Twig_Extension { return [ new \Twig_SimpleFunction('array', [$this, 'arrayFunc']), + new \Twig_SimpleFunction('array_key_value', [$this, 'arrayKeyValueFunc']), new \Twig_simpleFunction('authorize', [$this, 'authorize']), new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]), new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]), @@ -667,6 +668,26 @@ class TwigExtension extends \Twig_Extension return (array)$value; } + /** + * Workaround for twig associative array initialization + * Returns a key => val array + * + * @param string $key key of item + * @param string $val value of item + * @param string $current_array optional array to add to + * + * @return array + */ + public function arrayKeyValueFunc($key, $val, $current_array = null) + { + if (empty($current_array)) { + return array( $key => $val ); + } else { + $current_array[$key] = $val; + return $current_array; + } + } + /** * Returns a string from a value. If the value is array, return it json encoded * From 59fc6c20e8d171674874b3e6092e402c16ba9d61 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 19 Feb 2016 10:50:54 -0700 Subject: [PATCH 4/8] added a test for new Twig function --- .../Grav/Common/Twig/TwigExtensionTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/unit/Grav/Common/Twig/TwigExtensionTest.php diff --git a/tests/unit/Grav/Common/Twig/TwigExtensionTest.php b/tests/unit/Grav/Common/Twig/TwigExtensionTest.php new file mode 100644 index 000000000..0639ae308 --- /dev/null +++ b/tests/unit/Grav/Common/Twig/TwigExtensionTest.php @@ -0,0 +1,32 @@ +grav = Fixtures::get('grav'); + $this->twig_ext = new TwigExtension(); + } + + public function testArrayKeyValue() + { + $this->assertSame(['meat' => 'steak'], + $this->twig_ext->arrayKeyValueFunc('meat', 'steak')); + $this->assertSame(['fruit' => 'apple', 'meat' => 'steak'], + $this->twig_ext->arrayKeyValueFunc('meat', 'steak', ['fruit' => 'apple'])); + } + +} From 5ee4eafd5f13f74682f1da258f0541e144184883 Mon Sep 17 00:00:00 2001 From: Tyler Cosgrove Date: Fri, 19 Feb 2016 14:09:43 -0500 Subject: [PATCH 5/8] More Inflector Tests --- tests/unit/Grav/Common/InflectorTest.php | 27 ++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/unit/Grav/Common/InflectorTest.php b/tests/unit/Grav/Common/InflectorTest.php index e7c007209..0d74f7b40 100644 --- a/tests/unit/Grav/Common/InflectorTest.php +++ b/tests/unit/Grav/Common/InflectorTest.php @@ -105,22 +105,41 @@ class InflectorTest extends \Codeception\TestCase\Test public function testTableize() { - + $this->assertSame('people', $this->inflector->tableize('Person')); + $this->assertSame('pages', $this->inflector->tableize('Page')); + $this->assertSame('blog_pages', $this->inflector->tableize('BlogPage')); + $this->assertSame('admin_dependencies', $this->inflector->tableize('adminDependency')); + $this->assertSame('admin_dependencies', $this->inflector->tableize('admin-dependency')); + $this->assertSame('admin_dependencies', $this->inflector->tableize('admin_dependency')); } public function testClassify() { - + $this->assertSame('Person', $this->inflector->classify('people')); + $this->assertSame('Page', $this->inflector->classify('pages')); + $this->assertSame('BlogPage', $this->inflector->classify('blog_pages')); + $this->assertSame('AdminDependency', $this->inflector->classify('admin_dependencies')); } public function testOrdinalize() { - + $this->assertSame('1st', $this->inflector->ordinalize(1)); + $this->assertSame('2nd', $this->inflector->ordinalize(2)); + $this->assertSame('3rd', $this->inflector->ordinalize(3)); + $this->assertSame('4th', $this->inflector->ordinalize(4)); + $this->assertSame('5th', $this->inflector->ordinalize(5)); + $this->assertSame('16th', $this->inflector->ordinalize(16)); + $this->assertSame('51st', $this->inflector->ordinalize(51)); + $this->assertSame('111th', $this->inflector->ordinalize(111)); + $this->assertSame('123rd', $this->inflector->ordinalize(123)); } public function testMonthize() { - + $this->assertSame(0, $this->inflector->monthize(10)); + $this->assertSame(1, $this->inflector->monthize(30)); + $this->assertSame(1, $this->inflector->monthize(41)); + $this->assertSame(11, $this->inflector->monthize(365)); } } From bd14963992c03a1be1904074f8b2b29a11307d39 Mon Sep 17 00:00:00 2001 From: Tyler Cosgrove Date: Sun, 21 Feb 2016 21:18:59 -0500 Subject: [PATCH 6/8] Add more TwigExtension tests --- .../Grav/Common/Twig/TwigExtensionTest.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/unit/Grav/Common/Twig/TwigExtensionTest.php b/tests/unit/Grav/Common/Twig/TwigExtensionTest.php index 0639ae308..686851267 100644 --- a/tests/unit/Grav/Common/Twig/TwigExtensionTest.php +++ b/tests/unit/Grav/Common/Twig/TwigExtensionTest.php @@ -29,4 +29,52 @@ class TwigExtensionTest extends \Codeception\TestCase\Test $this->twig_ext->arrayKeyValueFunc('meat', 'steak', ['fruit' => 'apple'])); } + public function testContainsFilter() + { + $this->assertTrue($this->twig_ext->containsFilter('grav','grav')); + $this->assertTrue($this->twig_ext->containsFilter('So, I found this new cms, called grav, and it\'s pretty awesome guys','grav')); + } + + public function testMd5Filter() + { + $this->assertSame(md5('grav'), $this->twig_ext->md5Filter('grav')); + $this->assertSame(md5('devs@getgrav.org'), $this->twig_ext->md5Filter('devs@getgrav.org')); + } + + public function testSafeEmailFilter() + { + $this->assertSame('devs@getgrav.org', $this->twig_ext->safeEmailFilter('devs@getgrav.org')); + $this->assertSame('someone@example.com', $this->twig_ext->safeEmailFilter('someone@example.com')); + } + + public function testRandomizeFilter() + { + $array = [1,2,3,4,5]; + $this->assertContains(2, $this->twig_ext->randomizeFilter($array)); + $this->assertSame($array, $this->twig_ext->randomizeFilter($array, 5)); + $this->assertSame($array[0], $this->twig_ext->randomizeFilter($array, 1)[0]); + $this->assertSame($array[3], $this->twig_ext->randomizeFilter($array, 4)[3]); + $this->assertSame($array[1], $this->twig_ext->randomizeFilter($array, 4)[1]); + } + + public function testModulusFilter() + { + $this->assertSame(3, $this->twig_ext->modulusFilter(3,4)); + $this->assertSame(1, $this->twig_ext->modulusFilter(11,2)); + $this->assertSame(0, $this->twig_ext->modulusFilter(10,2)); + $this->assertSame(2, $this->twig_ext->modulusFilter(10,4)); + } + + public function testInflectorFilter() + { + $this->assertSame('people', $this->twig_ext->inflectorFilter('plural', 'person')); + $this->assertSame('shoe', $this->twig_ext->inflectorFilter('singular', 'shoes')); + $this->assertSame('Welcome Page', $this->twig_ext->inflectorFilter('title', 'welcome page')); + $this->assertSame('SendEmail', $this->twig_ext->inflectorFilter('camel', 'send_email')); + $this->assertSame('camel_cased', $this->twig_ext->inflectorFilter('underscor', 'CamelCased')); + $this->assertSame('something-text', $this->twig_ext->inflectorFilter('hyphen', 'Something Text')); + $this->assertSame('Something text to read', $this->twig_ext->inflectorFilter('human', 'something_text_to_read')); + $this->assertSame(5, $this->twig_ext->inflectorFilter('month', 181)); + $this->assertSame('10th', $this->twig_ext->inflectorFilter('ordinal', 10)); + } } From 8051debdcc2ac7700c318cd335d80b8876405dd3 Mon Sep 17 00:00:00 2001 From: Tyler Cosgrove Date: Sun, 21 Feb 2016 21:22:19 -0500 Subject: [PATCH 7/8] Change Monthize doc All results turn 181 to 5 and not 6. --- system/src/Grav/Common/Twig/TwigExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index d2035d800..c1e7cefb3 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -218,7 +218,7 @@ class TwigExtension extends \Twig_Extension * {{ 'CamelCased'|underscorize }} => camel_cased * {{ 'Something Text'|hyphenize }} => something-text * {{ 'something_text_to_read'|humanize }} => "Something text to read" - * {{ '181'|monthize }} => 6 + * {{ '181'|monthize }} => 5 * {{ '10'|ordinalize }} => 10th * * @param string $action From 01af9dfc4649e203abc6c73af96e0125be3535f4 Mon Sep 17 00:00:00 2001 From: Tyler Cosgrove Date: Sun, 21 Feb 2016 22:18:26 -0500 Subject: [PATCH 8/8] More TwigExtension Tests --- .../Grav/Common/Twig/TwigExtensionTest.php | 143 +++++++++++++++--- 1 file changed, 125 insertions(+), 18 deletions(-) diff --git a/tests/unit/Grav/Common/Twig/TwigExtensionTest.php b/tests/unit/Grav/Common/Twig/TwigExtensionTest.php index 686851267..a0938838c 100644 --- a/tests/unit/Grav/Common/Twig/TwigExtensionTest.php +++ b/tests/unit/Grav/Common/Twig/TwigExtensionTest.php @@ -21,12 +21,29 @@ class TwigExtensionTest extends \Codeception\TestCase\Test $this->twig_ext = new TwigExtension(); } - public function testArrayKeyValue() + public function testInflectorFilter() { - $this->assertSame(['meat' => 'steak'], - $this->twig_ext->arrayKeyValueFunc('meat', 'steak')); - $this->assertSame(['fruit' => 'apple', 'meat' => 'steak'], - $this->twig_ext->arrayKeyValueFunc('meat', 'steak', ['fruit' => 'apple'])); + $this->assertSame('people', $this->twig_ext->inflectorFilter('plural', 'person')); + $this->assertSame('shoe', $this->twig_ext->inflectorFilter('singular', 'shoes')); + $this->assertSame('Welcome Page', $this->twig_ext->inflectorFilter('title', 'welcome page')); + $this->assertSame('SendEmail', $this->twig_ext->inflectorFilter('camel', 'send_email')); + $this->assertSame('camel_cased', $this->twig_ext->inflectorFilter('underscor', 'CamelCased')); + $this->assertSame('something-text', $this->twig_ext->inflectorFilter('hyphen', 'Something Text')); + $this->assertSame('Something text to read', $this->twig_ext->inflectorFilter('human', 'something_text_to_read')); + $this->assertSame(5, $this->twig_ext->inflectorFilter('month', 181)); + $this->assertSame('10th', $this->twig_ext->inflectorFilter('ordinal', 10)); + } + + public function testMd5Filter() + { + $this->assertSame(md5('grav'), $this->twig_ext->md5Filter('grav')); + $this->assertSame(md5('devs@getgrav.org'), $this->twig_ext->md5Filter('devs@getgrav.org')); + } + + public function testKsortFilter() + { + $object = array("name"=>"Bob","age"=>8,"colour"=>"red"); + $this->assertSame(array("age"=>8,"colour"=>"red","name"=>"Bob"), $this->twig_ext->ksortFilter($object)); } public function testContainsFilter() @@ -35,10 +52,25 @@ class TwigExtensionTest extends \Codeception\TestCase\Test $this->assertTrue($this->twig_ext->containsFilter('So, I found this new cms, called grav, and it\'s pretty awesome guys','grav')); } - public function testMd5Filter() + public function testNicetimeFilter() { - $this->assertSame(md5('grav'), $this->twig_ext->md5Filter('grav')); - $this->assertSame(md5('devs@getgrav.org'), $this->twig_ext->md5Filter('devs@getgrav.org')); + $now = time(); + $threeSeconds = time() - (3); + $threeMinutes = time() - (60*3); + $threeHours = time() - (60*60*3); + $threeDays = time() - (60*60*24*3); + $threeMonths = time() - (60*60*24*30*3); + $threeYears = time() - (60*60*24*365*3); + $measures = ['seconds','minutes','hours','days','months','years']; + + $this->assertSame('No date provided', $this->twig_ext->nicetimeFilter(null)); + + for ($i=0; $iassertSame('3 ' . $measures[$i] . ' ago', $this->twig_ext->nicetimeFilter($$time)); + } + + $this->assertSame('3 secs ago', $this->twig_ext->nicetimeFilter($threeSeconds, false)); } public function testSafeEmailFilter() @@ -65,16 +97,91 @@ class TwigExtensionTest extends \Codeception\TestCase\Test $this->assertSame(2, $this->twig_ext->modulusFilter(10,4)); } - public function testInflectorFilter() + public function testAbsoluteUrlFilter() { - $this->assertSame('people', $this->twig_ext->inflectorFilter('plural', 'person')); - $this->assertSame('shoe', $this->twig_ext->inflectorFilter('singular', 'shoes')); - $this->assertSame('Welcome Page', $this->twig_ext->inflectorFilter('title', 'welcome page')); - $this->assertSame('SendEmail', $this->twig_ext->inflectorFilter('camel', 'send_email')); - $this->assertSame('camel_cased', $this->twig_ext->inflectorFilter('underscor', 'CamelCased')); - $this->assertSame('something-text', $this->twig_ext->inflectorFilter('hyphen', 'Something Text')); - $this->assertSame('Something text to read', $this->twig_ext->inflectorFilter('human', 'something_text_to_read')); - $this->assertSame(5, $this->twig_ext->inflectorFilter('month', 181)); - $this->assertSame('10th', $this->twig_ext->inflectorFilter('ordinal', 10)); + + } + + public function testMarkdownFilter() + { + + } + + public function testStartsWithFilter() + { + + } + + public function testEndsWithFilter() + { + + } + + public function testDefinedDefaultFilter() + { + + } + + public function testRtrimFilter() + { + + } + + public function testLtrimFilter() + { + + } + + public function testRepeatFunc() + { + + } + + public function testUrlFunc() + { + + } + + public function testEvaluateFunc() + { + + } + + public function testDump() + { + + } + + public function testGistFunc() + { + + } + + public function testRandomStringFunc() + { + + } + + public function testPadFilter() + { + + } + + public function testArrayFunc() + { + + } + + public function testArrayKeyValue() + { + $this->assertSame(['meat' => 'steak'], + $this->twig_ext->arrayKeyValueFunc('meat', 'steak')); + $this->assertSame(['fruit' => 'apple', 'meat' => 'steak'], + $this->twig_ext->arrayKeyValueFunc('meat', 'steak', ['fruit' => 'apple'])); + } + + public function stringFunc() + { + } }