newest!
response
Respond2Respond
diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 185a2ca3c..580e59048 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -15,3 +15,6 @@ b2474ccd46f9374d52f00c8a195be2266463cf4f # Coding Style: Updated Spaces 73123a96d3fc055f08da036b0121b469a75e3371 + +# Tests indentation +f0216ce423bb133ce668774a5f75ca86455b7280 diff --git a/tests/assets/Sport.php b/tests/assets/Sport.php index fa1897064..d581985d8 100644 --- a/tests/assets/Sport.php +++ b/tests/assets/Sport.php @@ -1,9 +1,9 @@ get_loader(); - } + $TimberLoader = new Timber\Loader(); + for ($i = 0; $i < 5000; $i++) { + $loader = $TimberLoader->get_loader(); } + } - public static function testImageResize() - { - $img = 'arch.jpg'; + public static function testImageResize() + { + $img = 'arch.jpg'; + $upload_dir = wp_upload_dir(); + $destination = $upload_dir['path'] . '/' . $img; + if (!file_exists($destination)) { + copy(__DIR__ . '/../assets/' . $img, $destination); + } + for ($i = 0; $i < 20; $i++) { $upload_dir = wp_upload_dir(); - $destination = $upload_dir['path'] . '/' . $img; - if (!file_exists($destination)) { - copy(__DIR__ . '/../assets/' . $img, $destination); - } - for ($i = 0; $i < 20; $i++) { - $upload_dir = wp_upload_dir(); - $img = Timber\ImageHelper::resize($upload_dir['url'] . '/arch.jpg', 500, 200, 'default', true); - } + $img = Timber\ImageHelper::resize($upload_dir['url'] . '/arch.jpg', 500, 200, 'default', true); } + } - public static function run($function) - { - $start_time = microtime(true); - self::$function(); - $end_time = microtime(true); - echo $end_time - $start_time; - } + public static function run($function) + { + $start_time = microtime(true); + self::$function(); + $end_time = microtime(true); + echo $end_time - $start_time; } +} diff --git a/tests/benchmark/benchmark-timber.php b/tests/benchmark/benchmark-timber.php index 47aa36d15..15e9e7a27 100644 --- a/tests/benchmark/benchmark-timber.php +++ b/tests/benchmark/benchmark-timber.php @@ -1,20 +1,20 @@ get_loader(); - } + $TimberLoader = new Timber\Loader(); + for ($i = 0; $i < 5000; $i++) { + $loader = $TimberLoader->get_loader(); } + } - public static function run($function) - { - $start_time = microtime(true); - self::$function(); - $end_time = microtime(true); - echo $end_time - $start_time; - } + public static function run($function) + { + $start_time = microtime(true); + self::$function(); + $end_time = microtime(true); + echo $end_time - $start_time; } +} diff --git a/tests/php/timber-custom-comment.php b/tests/php/timber-custom-comment.php index e2f41645f..87ff1893f 100644 --- a/tests/php/timber-custom-comment.php +++ b/tests/php/timber-custom-comment.php @@ -1,9 +1,9 @@ factory->post->create([ - 'post_date' => $date . ' 19:46:41', - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives([ - 'type' => 'monthly', - 'show_year' => false, - 'limit' => 3, + $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-06-08', '2013-03-03']; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', ]); - $this->assertEquals(3, count($archives->items)); - $this->assertEquals(2, $archives->items[1]['post_count']); } + $this->go_to('/'); + $archives = new Timber\Archives([ + 'type' => 'monthly', + 'show_year' => false, + 'limit' => 3, + ]); + $this->assertEquals(3, count($archives->items)); + $this->assertEquals(2, $archives->items[1]['post_count']); + } - public function testArchiveMonthly() - { - $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-06-08']; - foreach ($dates as $date) { - $this->factory->post->create([ - 'post_date' => $date . ' 19:46:41', - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives([ - 'type' => 'monthly', - 'show_year' => false, - ]); - $this->assertEquals('December', $archives->items[0]['name']); - $this->assertEquals(3, count($archives->items)); - $this->assertEquals(2, $archives->items[1]['post_count']); - $archives = new Timber\Archives([ - 'type' => 'monthly', - 'show_year' => true, + public function testArchiveMonthly() + { + $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-06-08']; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', ]); - $this->assertEquals('December 2013', $archives->items[0]['name']); } + $this->go_to('/'); + $archives = new Timber\Archives([ + 'type' => 'monthly', + 'show_year' => false, + ]); + $this->assertEquals('December', $archives->items[0]['name']); + $this->assertEquals(3, count($archives->items)); + $this->assertEquals(2, $archives->items[1]['post_count']); + $archives = new Timber\Archives([ + 'type' => 'monthly', + 'show_year' => true, + ]); + $this->assertEquals('December 2013', $archives->items[0]['name']); + } - public function testArchiveYearly() - { - $dates = ['2011-11-08', '2011-12-08', '2013-11-09', '2014-07-04']; - foreach ($dates as $date) { - $this->factory->post->create([ - 'post_date' => $date . ' 19:46:41', - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives([ - 'type' => 'yearly', - 'show_year' => false, + public function testArchiveYearly() + { + $dates = ['2011-11-08', '2011-12-08', '2013-11-09', '2014-07-04']; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', ]); - $this->assertEquals(3, count($archives->items)); - $this->assertEquals(2, $archives->items[2]['post_count']); } + $this->go_to('/'); + $archives = new Timber\Archives([ + 'type' => 'yearly', + 'show_year' => false, + ]); + $this->assertEquals(3, count($archives->items)); + $this->assertEquals(2, $archives->items[2]['post_count']); + } - public function testArchiveDaily() - { - $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-11-09', '2013-06-08', '2014-01-08', - ]; - foreach ($dates as $date) { - $this->factory->post->create([ - 'post_date' => $date . ' 19:46:41', - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives([ - 'type' => 'daily', + public function testArchiveDaily() + { + $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-11-09', '2013-06-08', '2014-01-08', + ]; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', ]); - $this->assertEquals(5, count($archives->items)); - $this->assertEquals(2, $archives->items[2]['post_count']); } + $this->go_to('/'); + $archives = new Timber\Archives([ + 'type' => 'daily', + ]); + $this->assertEquals(5, count($archives->items)); + $this->assertEquals(2, $archives->items[2]['post_count']); + } - public function testArchiveYearlyMonthly() - { - $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-06-08', '2014-01-08', - ]; - foreach ($dates as $date) { - $this->factory->post->create([ - 'post_date' => $date . ' 19:46:41', - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives([ - 'type' => 'monthly-nested', - ]); - $this->assertEquals(2, count($archives->items)); - $this->assertEquals(4, $archives->items[1]['post_count']); - $this->assertEquals(2, $archives->items[1]['children'][1]['post_count']); - $archives = new Timber\Archives([ - 'type' => 'yearlymonthly', + public function testArchiveYearlyMonthly() + { + $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-06-08', '2014-01-08', + ]; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', ]); - $this->assertEquals(2, count($archives->items)); - $this->assertEquals(4, $archives->items[1]['post_count']); - $this->assertEquals(2, $archives->items[1]['children'][1]['post_count']); } + $this->go_to('/'); + $archives = new Timber\Archives([ + 'type' => 'monthly-nested', + ]); + $this->assertEquals(2, count($archives->items)); + $this->assertEquals(4, $archives->items[1]['post_count']); + $this->assertEquals(2, $archives->items[1]['children'][1]['post_count']); + $archives = new Timber\Archives([ + 'type' => 'yearlymonthly', + ]); + $this->assertEquals(2, count($archives->items)); + $this->assertEquals(4, $archives->items[1]['post_count']); + $this->assertEquals(2, $archives->items[1]['children'][1]['post_count']); + } - public function testArchiveWeekly() - { - $dates = ['2015-03-02', '2015-03-09', '2015-03-16', '2015-03-21', '2015-03-22', - ]; - foreach ($dates as $date) { - $this->factory->post->create([ - 'post_date' => $date . ' 19:46:41', - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives([ - 'type' => 'weekly', + public function testArchiveWeekly() + { + $dates = ['2015-03-02', '2015-03-09', '2015-03-16', '2015-03-21', '2015-03-22', + ]; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', ]); - $this->assertEquals(3, count($archives->items)); - $this->assertEquals(3, $archives->items[0]['post_count']); } + $this->go_to('/'); + $archives = new Timber\Archives([ + 'type' => 'weekly', + ]); + $this->assertEquals(3, count($archives->items)); + $this->assertEquals(3, $archives->items[0]['post_count']); + } - public function testArchiveAlpha() - { - $posts = [ - [ - 'date' => '2015-03-02', - 'post_title' => 'Jared loves Lauren', - ], - [ - 'date' => '2015-03-02', - 'post_title' => 'Another fantastic post', - ], - [ - 'date' => '2015-03-02', - 'post_title' => 'Foobar', - ], - [ - 'date' => '2015-03-02', - 'post_title' => 'Quack Quack', - ], - ]; - foreach ($posts as $post) { - $this->factory->post->create([ - 'post_date' => $post['date'] . ' 19:46:41', - 'post_title' => $post['post_title'], - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives([ - 'type' => 'alpha', + public function testArchiveAlpha() + { + $posts = [ + [ + 'date' => '2015-03-02', + 'post_title' => 'Jared loves Lauren', + ], + [ + 'date' => '2015-03-02', + 'post_title' => 'Another fantastic post', + ], + [ + 'date' => '2015-03-02', + 'post_title' => 'Foobar', + ], + [ + 'date' => '2015-03-02', + 'post_title' => 'Quack Quack', + ], + ]; + foreach ($posts as $post) { + $this->factory->post->create([ + 'post_date' => $post['date'] . ' 19:46:41', + 'post_title' => $post['post_title'], ]); - $this->assertEquals(4, count($archives->items)); - $this->assertEquals('Quack Quack', $archives->items[3]['name']); } + $this->go_to('/'); + $archives = new Timber\Archives([ + 'type' => 'alpha', + ]); + $this->assertEquals(4, count($archives->items)); + $this->assertEquals('Quack Quack', $archives->items[3]['name']); + } - public function testArchivesWithArgs() - { - register_post_type('book'); - $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-06-08', '2014-01-08', - ]; - foreach ($dates as $date) { - $this->factory->post->create([ - 'post_date' => $date . ' 19:46:41', - ]); - } - $dates = ['2014-11-08', '2014-12-08', '2014-11-09', '2014-06-08', '2015-01-08', '2015-02-14', - ]; - foreach ($dates as $date) { - $this->factory->post->create([ - 'post_date' => $date . ' 19:46:41', - 'post_type' => 'book', - ]); - } - $this->go_to('/'); - $archives = new Timber\Archives(); - - $this->assertEquals(2, count($archives->items)); - $archives = new Timber\Archives([ + public function testArchivesWithArgs() + { + register_post_type('book'); + $dates = ['2013-11-08', '2013-12-08', '2013-11-09', '2013-06-08', '2014-01-08', + ]; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', + ]); + } + $dates = ['2014-11-08', '2014-12-08', '2014-11-09', '2014-06-08', '2015-01-08', '2015-02-14', + ]; + foreach ($dates as $date) { + $this->factory->post->create([ + 'post_date' => $date . ' 19:46:41', 'post_type' => 'book', - 'type' => 'monthly', ]); - $this->assertEquals(5, count($archives->items)); } + $this->go_to('/'); + $archives = new Timber\Archives(); + + $this->assertEquals(2, count($archives->items)); + $archives = new Timber\Archives([ + 'post_type' => 'book', + 'type' => 'monthly', + ]); + $this->assertEquals(5, count($archives->items)); } +} diff --git a/tests/test-timber-cache.php b/tests/test-timber-cache.php index a6047d20a..90e1592e4 100644 --- a/tests/test-timber-cache.php +++ b/tests/test-timber-cache.php @@ -1,528 +1,528 @@ _generate_transient_name(); - Timber\Helper::_lock_transient($transient, 5); - $this->assertTrue(Timber\Helper::_is_transient_locked($transient)); - } +use Timber\Cache\TimberKeyGeneratorInterface; + +/** + * @group called-post-constructor + */ +class TestTimberCache extends Timber_UnitTestCase +{ + private function _generate_transient_name() + { + static $i = 0; + $i++; + return 'timber_test_transient_' . $i; + } - public function testTransientUnlock() - { - $transient = $this->_generate_transient_name(); - Timber\Helper::_lock_transient($transient, 5); - Timber\Helper::_unlock_transient($transient, 5); - $this->assertFalse(Timber\Helper::_is_transient_locked($transient)); - } + public function testTransientLock() + { + $transient = $this->_generate_transient_name(); + Timber\Helper::_lock_transient($transient, 5); + $this->assertTrue(Timber\Helper::_is_transient_locked($transient)); + } - public function testTransientExpire() - { - $transient = $this->_generate_transient_name(); + public function testTransientUnlock() + { + $transient = $this->_generate_transient_name(); + Timber\Helper::_lock_transient($transient, 5); + Timber\Helper::_unlock_transient($transient, 5); + $this->assertFalse(Timber\Helper::_is_transient_locked($transient)); + } - Timber\Helper::_lock_transient($transient, 1); - sleep(2); - $this->assertFalse(Timber\Helper::_is_transient_locked($transient)); - } + public function testTransientExpire() + { + $transient = $this->_generate_transient_name(); - public function testTransientLocksInternal() - { - $transient = $this->_generate_transient_name(); + Timber\Helper::_lock_transient($transient, 1); + sleep(2); + $this->assertFalse(Timber\Helper::_is_transient_locked($transient)); + } - $is_locked = Timber\Helper::transient($transient, function () use ($transient) { - return Timber\Helper::_is_transient_locked($transient); - }, 30); + public function testTransientLocksInternal() + { + $transient = $this->_generate_transient_name(); - $this->assertTrue($is_locked); - } + $is_locked = Timber\Helper::transient($transient, function () use ($transient) { + return Timber\Helper::_is_transient_locked($transient); + }, 30); - public function testTransientLocksExternal() - { - $transient = $this->_generate_transient_name(); + $this->assertTrue($is_locked); + } - Timber\Helper::_lock_transient($transient, 30); - $get_transient = Timber\Helper::transient($transient, '__return_true', 30); + public function testTransientLocksExternal() + { + $transient = $this->_generate_transient_name(); - $this->assertFalse($get_transient); - } + Timber\Helper::_lock_transient($transient, 30); + $get_transient = Timber\Helper::transient($transient, '__return_true', 30); - public function testTransientAsAnonymousFunction() - { - $transient = $this->_generate_transient_name(); + $this->assertFalse($get_transient); + } - $result = Timber\Helper::transient($transient, function () { - return 'pooptime'; - }, 200); - $this->assertEquals($result, 'pooptime'); - } + public function testTransientAsAnonymousFunction() + { + $transient = $this->_generate_transient_name(); - public function testSetTransient() - { - $transient = $this->_generate_transient_name(); + $result = Timber\Helper::transient($transient, function () { + return 'pooptime'; + }, 200); + $this->assertEquals($result, 'pooptime'); + } - $first_value = Timber\Helper::transient($transient, function () { - return 'first_value'; - }, 30); + public function testSetTransient() + { + $transient = $this->_generate_transient_name(); - $second_value = Timber\Helper::transient($transient, function () { - return 'second_value'; - }, 30); + $first_value = Timber\Helper::transient($transient, function () { + return 'first_value'; + }, 30); - $this->assertEquals('first_value', $second_value); - } + $second_value = Timber\Helper::transient($transient, function () { + return 'second_value'; + }, 30); - public function testDisableTransients() - { - $transient = $this->_generate_transient_name(); + $this->assertEquals('first_value', $second_value); + } - $first_value = Timber\Helper::transient($transient, function () { - return 'first_value'; - }, 30); + public function testDisableTransients() + { + $transient = $this->_generate_transient_name(); - $second_value = Timber\Helper::transient($transient, function () { - return 'second_value'; - }, false); + $first_value = Timber\Helper::transient($transient, function () { + return 'first_value'; + }, 30); - $this->assertEquals('second_value', $second_value); - } + $second_value = Timber\Helper::transient($transient, function () { + return 'second_value'; + }, false); - public function testTransientAsString() - { - $transient = $this->_generate_transient_name(); + $this->assertEquals('second_value', $second_value); + } - $result = Timber\Helper::transient($transient, 'my_test_callback', 200); - $this->assertEquals($result, 'lbj'); - } + public function testTransientAsString() + { + $transient = $this->_generate_transient_name(); - public function testTransientLocked() - { - $transient = $this->_generate_transient_name(); + $result = Timber\Helper::transient($transient, 'my_test_callback', 200); + $this->assertEquals($result, 'lbj'); + } - Timber\Helper::_lock_transient($transient, 30); + public function testTransientLocked() + { + $transient = $this->_generate_transient_name(); - // Transient is locked and won't be forced, so it should return false - $get_transient = Timber\Helper::transient($transient, '__return_true'); + Timber\Helper::_lock_transient($transient, 30); - $this->assertFalse($get_transient); - } + // Transient is locked and won't be forced, so it should return false + $get_transient = Timber\Helper::transient($transient, '__return_true'); - public function testTransientForce() - { - $transient = $this->_generate_transient_name(); + $this->assertFalse($get_transient); + } - Timber\Helper::_lock_transient($transient, 30); - $get_transient = Timber\Helper::transient($transient, '__return_true', 0, 5, true); + public function testTransientForce() + { + $transient = $this->_generate_transient_name(); - $this->assertTrue($get_transient); - } + Timber\Helper::_lock_transient($transient, 30); + $get_transient = Timber\Helper::transient($transient, '__return_true', 0, 5, true); - public function testTransientForceAllFilter() - { - $transient = $this->_generate_transient_name(); + $this->assertTrue($get_transient); + } - Timber\Helper::_lock_transient($transient, 30); + public function testTransientForceAllFilter() + { + $transient = $this->_generate_transient_name(); - add_filter('timber/transient/force_transients', '__return_true'); - $get_transient = Timber\Helper::transient($transient, '__return_true'); - remove_filter('timber/transient/force_transients', '__return_true'); + Timber\Helper::_lock_transient($transient, 30); - $this->assertTrue($get_transient); - } + add_filter('timber/transient/force_transients', '__return_true'); + $get_transient = Timber\Helper::transient($transient, '__return_true'); + remove_filter('timber/transient/force_transients', '__return_true'); - public function testKeyGenerator() - { - $post_id = $this->factory->post->create([ - 'post_title' => 'My Test Post', - ]); - $post = Timber::get_post($post_id); + $this->assertTrue($get_transient); + } - $kg = new Timber\Cache\KeyGenerator(); - $key = $kg->generateKey($post); + public function testKeyGenerator() + { + $post_id = $this->factory->post->create([ + 'post_title' => 'My Test Post', + ]); + $post = Timber::get_post($post_id); - $this->assertStringStartsWith('Timber;Post;', $key); - } + $kg = new Timber\Cache\KeyGenerator(); + $key = $kg->generateKey($post); - public function testKeyGeneratorWithTimberKeyGeneratorInterface() - { - $kg = new Timber\Cache\KeyGenerator(); - $thing = new MyFakeThing(); - $key = $kg->generateKey($thing); - $this->assertEquals('iamakey', $key); - } + $this->assertStringStartsWith('Timber;Post;', $key); + } - public function testKeyGeneratorWithArray() - { - $kg = new Timber\Cache\KeyGenerator(); - $thing = [ - '_cache_key' => 'iAmAKeyButInAnArray', - ]; - $key = $kg->generateKey($thing); - $this->assertEquals('iAmAKeyButInAnArray', $key); - } + public function testKeyGeneratorWithTimberKeyGeneratorInterface() + { + $kg = new Timber\Cache\KeyGenerator(); + $thing = new MyFakeThing(); + $key = $kg->generateKey($thing); + $this->assertEquals('iamakey', $key); + } - public function testTransientForceFilter() - { - $transient = $this->_generate_transient_name(); + public function testKeyGeneratorWithArray() + { + $kg = new Timber\Cache\KeyGenerator(); + $thing = [ + '_cache_key' => 'iAmAKeyButInAnArray', + ]; + $key = $kg->generateKey($thing); + $this->assertEquals('iAmAKeyButInAnArray', $key); + } - Timber\Helper::_lock_transient($transient, 30); + public function testTransientForceFilter() + { + $transient = $this->_generate_transient_name(); - add_filter('timber_force_transient_' . $transient, '__return_true'); - $get_transient = Timber\Helper::transient($transient, '__return_true'); - remove_filter('timber_force_transient_' . $transient, '__return_true'); + Timber\Helper::_lock_transient($transient, 30); - $this->assertTrue($get_transient); - } + add_filter('timber_force_transient_' . $transient, '__return_true'); + $get_transient = Timber\Helper::transient($transient, '__return_true'); + remove_filter('timber_force_transient_' . $transient, '__return_true'); - public function testExpireTransient() - { - $transient = $this->_generate_transient_name(); + $this->assertTrue($get_transient); + } - $first_value = Timber\Helper::transient($transient, function () { - return 'first_value'; - }, 1); + public function testExpireTransient() + { + $transient = $this->_generate_transient_name(); - sleep(2); + $first_value = Timber\Helper::transient($transient, function () { + return 'first_value'; + }, 1); - $second_value = Timber\Helper::transient($transient, function () { - return 'second_value'; - }, 1); + sleep(2); - $this->assertEquals('second_value', $second_value); - } + $second_value = Timber\Helper::transient($transient, function () { + return 'second_value'; + }, 1); - /** - * @expectedDeprecated Timber::$cache and Timber::$twig_cache - */ - public function testTwigCacheDeprecated() - { - $cache_dir = __DIR__ . '/../cache/twig'; - if (is_dir($cache_dir)) { - Timber\Loader::rrmdir($cache_dir); - } - $this->assertFileDoesNotExist($cache_dir); - Timber::$twig_cache = true; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - Timber::compile('assets/single-post.twig', [ - 'post' => $post, - ]); - sleep(1); - $this->assertFileExists($cache_dir); - $loader = new Timber\Loader(); - $loader->clear_cache_twig(); - Timber::$twig_cache = false; - $this->assertFileDoesNotExist($cache_dir); - } + $this->assertEquals('second_value', $second_value); + } - /** - * @expectedDeprecated Timber::$cache and Timber::$twig_cache - */ - public function testTwigCacheAliasDeprecated() - { - $cache_dir = __DIR__ . '/../cache/twig'; - if (is_dir($cache_dir)) { - Timber\Loader::rrmdir($cache_dir); - } - $this->assertFileDoesNotExist($cache_dir); - Timber::$cache = true; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - Timber::compile('assets/single-post.twig', [ - 'post' => $post, - ]); - //sleep(1); - $this->assertFileExists($cache_dir); - $loader = new Timber\Loader(); - $loader->clear_cache_twig(); - Timber::$cache = false; - Timber::$twig_cache = false; - $this->assertFileDoesNotExist($cache_dir); - } + /** + * @expectedDeprecated Timber::$cache and Timber::$twig_cache + */ + public function testTwigCacheDeprecated() + { + $cache_dir = __DIR__ . '/../cache/twig'; + if (is_dir($cache_dir)) { + Timber\Loader::rrmdir($cache_dir); + } + $this->assertFileDoesNotExist($cache_dir); + Timber::$twig_cache = true; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + Timber::compile('assets/single-post.twig', [ + 'post' => $post, + ]); + sleep(1); + $this->assertFileExists($cache_dir); + $loader = new Timber\Loader(); + $loader->clear_cache_twig(); + Timber::$twig_cache = false; + $this->assertFileDoesNotExist($cache_dir); + } - public function testTwigCache() - { - $cache_dir = __DIR__ . '/../cache/twig'; + /** + * @expectedDeprecated Timber::$cache and Timber::$twig_cache + */ + public function testTwigCacheAliasDeprecated() + { + $cache_dir = __DIR__ . '/../cache/twig'; + if (is_dir($cache_dir)) { + Timber\Loader::rrmdir($cache_dir); + } + $this->assertFileDoesNotExist($cache_dir); + Timber::$cache = true; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + Timber::compile('assets/single-post.twig', [ + 'post' => $post, + ]); + //sleep(1); + $this->assertFileExists($cache_dir); + $loader = new Timber\Loader(); + $loader->clear_cache_twig(); + Timber::$cache = false; + Timber::$twig_cache = false; + $this->assertFileDoesNotExist($cache_dir); + } - if (is_dir($cache_dir)) { - Timber\Loader::rrmdir($cache_dir); - } + public function testTwigCache() + { + $cache_dir = __DIR__ . '/../cache/twig'; - $this->assertFileDoesNotExist($cache_dir); + if (is_dir($cache_dir)) { + Timber\Loader::rrmdir($cache_dir); + } - $cache_enabler = function ($options) { - $options['cache'] = true; + $this->assertFileDoesNotExist($cache_dir); - return $options; - }; + $cache_enabler = function ($options) { + $options['cache'] = true; - add_filter('timber/twig/environment/options', $cache_enabler); + return $options; + }; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - Timber::compile('assets/single-post.twig', [ - 'post' => $post, - ]); - sleep(1); + add_filter('timber/twig/environment/options', $cache_enabler); - $this->assertFileExists($cache_dir); + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + Timber::compile('assets/single-post.twig', [ + 'post' => $post, + ]); + sleep(1); - $loader = new Timber\Loader(); - $loader->clear_cache_twig(); - $this->assertFileDoesNotExist($cache_dir); + $this->assertFileExists($cache_dir); - remove_filter('timber/twig/environment/options', $cache_enabler); - } + $loader = new Timber\Loader(); + $loader->clear_cache_twig(); + $this->assertFileDoesNotExist($cache_dir); - public function testTimberLoaderCache() - { - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $str_old = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - ], 600); - $str_another = Timber::compile('assets/single-parent.twig', [ - 'post' => $post, - 'rand' => rand(0, 99), - ], 500); - //sleep(1); - $str_new = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - ], 600); - $this->assertEquals($str_old, $str_new); - $loader = new Timber\Loader(); - $clear = $loader->clear_cache_timber(); - $this->assertGreaterThan(0, $clear); - global $wpdb; - $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; - $wpdb->query($query); - $this->assertEquals(0, $wpdb->num_rows); - } + remove_filter('timber/twig/environment/options', $cache_enabler); + } - public function testTimberLoaderCacheObject() - { - global $_wp_using_ext_object_cache; - global $wp_object_cache; - $_wp_using_ext_object_cache = true; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $str_old = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - ], 600, \Timber\Loader::CACHE_OBJECT); - //sleep(1); - $str_new = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - ], 600, \Timber\Loader::CACHE_OBJECT); - $this->assertEquals($str_old, $str_new); - $loader = new Timber\Loader(); - $clear = $loader->clear_cache_timber(\Timber\Loader::CACHE_OBJECT); - $this->assertTrue($clear); - $works = true; - if (isset($wp_object_cache->cache[\Timber\Loader::CACHEGROUP]) - && !empty($wp_object_cache->cache[\Timber\Loader::CACHEGROUP])) { - $works = false; - } - $this->assertTrue($works); - } + public function testTimberLoaderCache() + { + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $str_old = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + ], 600); + $str_another = Timber::compile('assets/single-parent.twig', [ + 'post' => $post, + 'rand' => rand(0, 99), + ], 500); + //sleep(1); + $str_new = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + ], 600); + $this->assertEquals($str_old, $str_new); + $loader = new Timber\Loader(); + $clear = $loader->clear_cache_timber(); + $this->assertGreaterThan(0, $clear); + global $wpdb; + $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; + $wpdb->query($query); + $this->assertEquals(0, $wpdb->num_rows); + } - public function tear_down() - { - global $_wp_using_ext_object_cache; - $_wp_using_ext_object_cache = false; - global $wpdb; - $query = "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; - $wpdb->query($query); - parent::tear_down(); - } + public function testTimberLoaderCacheObject() + { + global $_wp_using_ext_object_cache; + global $wp_object_cache; + $_wp_using_ext_object_cache = true; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $str_old = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + ], 600, \Timber\Loader::CACHE_OBJECT); + //sleep(1); + $str_new = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + ], 600, \Timber\Loader::CACHE_OBJECT); + $this->assertEquals($str_old, $str_new); + $loader = new Timber\Loader(); + $clear = $loader->clear_cache_timber(\Timber\Loader::CACHE_OBJECT); + $this->assertTrue($clear); + $works = true; + if (isset($wp_object_cache->cache[\Timber\Loader::CACHEGROUP]) + && !empty($wp_object_cache->cache[\Timber\Loader::CACHEGROUP])) { + $works = false; + } + $this->assertTrue($works); + } - public function testTimberLoaderCacheTransients() - { - $time = 1; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $str_old = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - 'rand' => rand(0, 99999), - ], $time); - sleep(2); - $str_new = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - 'rand' => rand(0, 99999), - ], $time); - $this->assertEquals($str_old, $str_new); - global $wpdb; - $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; - $data = $wpdb->get_results($query); - $this->assertEquals(1, $wpdb->num_rows); - } + public function tear_down() + { + global $_wp_using_ext_object_cache; + $_wp_using_ext_object_cache = false; + global $wpdb; + $query = "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; + $wpdb->query($query); + parent::tear_down(); + } - public function testTimberLoaderCacheTransientsAdminLoggedIn() - { - wp_set_current_user(1); - $time = 1; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $r1 = rand(0, 999999); - $r2 = rand(0, 999999); - $str_old = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r1, - ], [600, false]); - self::_swapFiles(); - $str_new = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r2, - ], [600, false]); - $this->assertNotEquals($str_old, $str_new); - self::_unswapFiles(); - } + public function testTimberLoaderCacheTransients() + { + $time = 1; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $str_old = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + 'rand' => rand(0, 99999), + ], $time); + sleep(2); + $str_new = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + 'rand' => rand(0, 99999), + ], $time); + $this->assertEquals($str_old, $str_new); + global $wpdb; + $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; + $data = $wpdb->get_results($query); + $this->assertEquals(1, $wpdb->num_rows); + } - public function _swapFiles() - { - rename(__DIR__ . '/assets/single-post-rand.twig', __DIR__ . '/assets/single-post-rand.twig.tmp'); - rename(__DIR__ . '/assets/relative.twig', __DIR__ . '/assets/single-post-rand.twig'); - } + public function testTimberLoaderCacheTransientsAdminLoggedIn() + { + wp_set_current_user(1); + $time = 1; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $r1 = rand(0, 999999); + $r2 = rand(0, 999999); + $str_old = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r1, + ], [600, false]); + self::_swapFiles(); + $str_new = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r2, + ], [600, false]); + $this->assertNotEquals($str_old, $str_new); + self::_unswapFiles(); + } - public function _unswapFiles() - { - rename(__DIR__ . '/assets/single-post-rand.twig', __DIR__ . '/assets/relative.twig'); - rename(__DIR__ . '/assets/single-post-rand.twig.tmp', __DIR__ . '/assets/single-post-rand.twig'); - } + public function _swapFiles() + { + rename(__DIR__ . '/assets/single-post-rand.twig', __DIR__ . '/assets/single-post-rand.twig.tmp'); + rename(__DIR__ . '/assets/relative.twig', __DIR__ . '/assets/single-post-rand.twig'); + } - public function testTimberLoaderCacheTransientsAdminLoggedOut() - { - $time = 1; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $r1 = rand(0, 999999); - $str_old = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r1, - ], [600, false]); - self::_swapFiles(); - $str_new = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r1, - ], [600, false]); - $this->assertEquals($str_old, $str_new); - self::_unswapFiles(); - } + public function _unswapFiles() + { + rename(__DIR__ . '/assets/single-post-rand.twig', __DIR__ . '/assets/relative.twig'); + rename(__DIR__ . '/assets/single-post-rand.twig.tmp', __DIR__ . '/assets/single-post-rand.twig'); + } - public function testTimberLoaderCacheTransientsAdminLoggedOutWithSiteCache() - { - $time = 1; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $r1 = rand(0, 999999); - $str_old = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r1, - ], [600, false], \Timber\Loader::CACHE_SITE_TRANSIENT); - self::_swapFiles(); - $str_new = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r1, - ], [600, false], \Timber\Loader::CACHE_SITE_TRANSIENT); - $this->assertEquals($str_old, $str_new); - self::_unswapFiles(); - } + public function testTimberLoaderCacheTransientsAdminLoggedOut() + { + $time = 1; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $r1 = rand(0, 999999); + $str_old = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r1, + ], [600, false]); + self::_swapFiles(); + $str_new = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r1, + ], [600, false]); + $this->assertEquals($str_old, $str_new); + self::_unswapFiles(); + } - public function testTimberLoaderCacheTransientsAdminLoggedOutWithObjectCache() - { - global $_wp_using_ext_object_cache; - $_wp_using_ext_object_cache = true; - $time = 1; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $r1 = rand(0, 999999); - $str_old = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r1, - ], [600, false], \Timber\Loader::CACHE_OBJECT); - self::_swapFiles(); - $str_new = Timber::compile('assets/single-post-rand.twig', [ - 'post' => $post, - 'rand' => $r1, - ], [600, false], \Timber\Loader::CACHE_OBJECT); - $this->assertEquals($str_old, $str_new); - self::_unswapFiles(); - $_wp_using_ext_object_cache = false; - } + public function testTimberLoaderCacheTransientsAdminLoggedOutWithSiteCache() + { + $time = 1; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $r1 = rand(0, 999999); + $str_old = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r1, + ], [600, false], \Timber\Loader::CACHE_SITE_TRANSIENT); + self::_swapFiles(); + $str_new = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r1, + ], [600, false], \Timber\Loader::CACHE_SITE_TRANSIENT); + $this->assertEquals($str_old, $str_new); + self::_unswapFiles(); + } - public function testTimberLoaderCacheTransientsWithExtObjectCache() - { - global $_wp_using_ext_object_cache; - $_wp_using_ext_object_cache = true; - $time = 1; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $r1 = rand(0, 999999); - $r2 = rand(0, 999999); - $str_old = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - 'rand' => $r1, - ], $time); - $str_new = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - 'rand' => $r2, - ], $time); - $this->assertEquals($str_old, $str_new); - global $wpdb; - $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; - $data = $wpdb->get_results($query); - $this->assertEquals(0, $wpdb->num_rows); - $_wp_using_ext_object_cache = false; - } + public function testTimberLoaderCacheTransientsAdminLoggedOutWithObjectCache() + { + global $_wp_using_ext_object_cache; + $_wp_using_ext_object_cache = true; + $time = 1; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $r1 = rand(0, 999999); + $str_old = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r1, + ], [600, false], \Timber\Loader::CACHE_OBJECT); + self::_swapFiles(); + $str_new = Timber::compile('assets/single-post-rand.twig', [ + 'post' => $post, + 'rand' => $r1, + ], [600, false], \Timber\Loader::CACHE_OBJECT); + $this->assertEquals($str_old, $str_new); + self::_unswapFiles(); + $_wp_using_ext_object_cache = false; + } - public function testTimberLoaderCacheTransientsButKeepOtherTransients() - { - $time = 1; - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - set_transient('random_600', 'foo', 600); - $random_post = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - 'rand' => rand(0, 99999), - ], 600); - $str_old = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - 'rand' => rand(0, 99999), - ], $time); - sleep(2); - $str_new = Timber::compile('assets/single-post.twig', [ - 'post' => $post, - 'rand' => rand(0, 99999), - ], $time); - $this->assertEquals($str_old, $str_new); - global $wpdb; - $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; - $data = $wpdb->get_results($query); - $this->assertEquals(2, $wpdb->num_rows); - $this->assertEquals('foo', get_transient('random_600')); - } + public function testTimberLoaderCacheTransientsWithExtObjectCache() + { + global $_wp_using_ext_object_cache; + $_wp_using_ext_object_cache = true; + $time = 1; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $r1 = rand(0, 999999); + $r2 = rand(0, 999999); + $str_old = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + 'rand' => $r1, + ], $time); + $str_new = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + 'rand' => $r2, + ], $time); + $this->assertEquals($str_old, $str_new); + global $wpdb; + $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; + $data = $wpdb->get_results($query); + $this->assertEquals(0, $wpdb->num_rows); + $_wp_using_ext_object_cache = false; } - class MyFakeThing implements TimberKeyGeneratorInterface + public function testTimberLoaderCacheTransientsButKeepOtherTransients() { - public function _get_cache_key() - { - return 'iamakey'; - } + $time = 1; + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + set_transient('random_600', 'foo', 600); + $random_post = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + 'rand' => rand(0, 99999), + ], 600); + $str_old = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + 'rand' => rand(0, 99999), + ], $time); + sleep(2); + $str_new = Timber::compile('assets/single-post.twig', [ + 'post' => $post, + 'rand' => rand(0, 99999), + ], $time); + $this->assertEquals($str_old, $str_new); + global $wpdb; + $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE '_transient_timberloader_%'"; + $data = $wpdb->get_results($query); + $this->assertEquals(2, $wpdb->num_rows); + $this->assertEquals('foo', get_transient('random_600')); } +} - function my_test_callback() +class MyFakeThing implements TimberKeyGeneratorInterface +{ + public function _get_cache_key() { - return "lbj"; + return 'iamakey'; } +} + +function my_test_callback() +{ + return "lbj"; +} diff --git a/tests/test-timber-comment-avatar.php b/tests/test-timber-comment-avatar.php index 01a2538da..9438993f7 100644 --- a/tests/test-timber-comment-avatar.php +++ b/tests/test-timber-comment-avatar.php @@ -1,168 +1,168 @@ markTestSkipped('Cannot test avatar images when not connected to internet'); - } - $post_id = $this->factory->post->create(); - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - ]); - $comment = Timber\Timber::get_comment($comment_id); - - # test default gravatr holding image - $avatar = $comment->avatar("mystery"); - - $this->assertTrue(substr($avatar, 0, 5) == "http:"); + if (!TestTimberImage::is_connected()) { + $this->markTestSkipped('Cannot test avatar images when not connected to internet'); } + $post_id = $this->factory->post->create(); + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + ]); + $comment = Timber\Timber::get_comment($comment_id); - public function testAvatarFalse() - { - update_option('show_avatars', false); - $post_id = $this->factory->post->create(); - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - ]); - $comment = Timber\Timber::get_comment($comment_id); + # test default gravatr holding image + $avatar = $comment->avatar("mystery"); - # test default gravatr holding image - $avatar = $comment->avatar(); + $this->assertTrue(substr($avatar, 0, 5) == "http:"); + } - $this->assertFalse($avatar); - } + public function testAvatarFalse() + { + update_option('show_avatars', false); + $post_id = $this->factory->post->create(); + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + ]); + $comment = Timber\Timber::get_comment($comment_id); - public function testAvatarBlank() - { - if (!TestTimberImage::is_connected()) { - $this->markTestSkipped('Cannot test avatar images when not connected to internet'); - } - $post_id = $this->factory->post->create(); - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - ]); - $comment = Timber\Timber::get_comment($comment_id); - - # test default gravatr holding image - $avatar = $comment->avatar(92, "blank"); - - $this->assertTrue(substr($avatar, 0, 5) == "http:"); - } + # test default gravatr holding image + $avatar = $comment->avatar(); + + $this->assertFalse($avatar); + } - public function testAvatarGravatarDefault() - { - if (!TestTimberImage::is_connected()) { - $this->markTestSkipped('Cannot test avatar images when not connected to internet'); - } - $post_id = $this->factory->post->create(); - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - ]); - $comment = Timber\Timber::get_comment($comment_id); - - # test default gravatr holding image - $avatar = $comment->avatar(92, "gravatar_default"); - - $this->assertTrue(substr($avatar, 0, 5) == "http:"); + public function testAvatarBlank() + { + if (!TestTimberImage::is_connected()) { + $this->markTestSkipped('Cannot test avatar images when not connected to internet'); } + $post_id = $this->factory->post->create(); + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + ]); + $comment = Timber\Timber::get_comment($comment_id); + + # test default gravatr holding image + $avatar = $comment->avatar(92, "blank"); + + $this->assertTrue(substr($avatar, 0, 5) == "http:"); + } - public function testGravatar() - { - if (!TestTimberImage::is_connected()) { - $this->markTestSkipped('Cannot test avatar images when not connected to internet'); - } - $post_id = $this->factory->post->create(); - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - 'comment_author' => 'jarednova', - 'comment_author_email' => 'jarednova@upstatement.com', - ]); - $comment = Timber\Timber::get_comment($comment_id); - $gravatar = md5(file_get_contents($comment->avatar())); - /* this keeps changing b/c of compression tweaks on WP.org, disabling the test */ - //$this->assertEquals($gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg'))); - - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - 'comment_author' => 'jarednova', - 'comment_author_email' => 'notjared@upstatement.com', - ]); - $comment = Timber\Timber::get_comment($comment_id); - $not_gravatar = md5(file_get_contents($comment->avatar())); - $this->assertNotEquals($not_gravatar, md5(file_get_contents(dirname(__FILE__) . '/assets/jarednova.jpeg'))); + public function testAvatarGravatarDefault() + { + if (!TestTimberImage::is_connected()) { + $this->markTestSkipped('Cannot test avatar images when not connected to internet'); } + $post_id = $this->factory->post->create(); + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + ]); + $comment = Timber\Timber::get_comment($comment_id); + + # test default gravatr holding image + $avatar = $comment->avatar(92, "gravatar_default"); - public function testAvatarSimple() - { - if (!TestTimberImage::is_connected()) { - $this->markTestSkipped('Cannot test avatar images when not connected to internet'); - } - $theme_url = get_theme_root_uri() . '/' . get_stylesheet(); - $post_id = $this->factory->post->create(); - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - ]); - $comment = Timber\Timber::get_comment($comment_id); - - # test default gravatr holding image - $avatar = $comment->avatar(32, "mystery"); - - $this->assertTrue(substr($avatar, 0, 5) == "http:"); - - # does it work if its SSL? - $_SERVER['HTTPS'] = 'on'; - $avatar = $comment->avatar(32, "mystery"); - $this->assertTrue(200 === $this->crawl($avatar)); - $this->assertTrue(substr($avatar, 0, 6) == "https:"); - $_SERVER['HTTPS'] = 'off'; - - # pass custom url on different domain. can't check by crawling as - # i get a 302 regardless of default url - # so just check it comes back with it in the url - $this->valid_avatar($comment, "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png"); - - # same domain. - $this->valid_avatar($comment, $theme_url . "/images/default.png"); - - #relative - $default_url = "/images/default.png"; - $avatar = $comment->avatar(32, $default_url); - if (strstr($avatar, '?')) { - list($url, $params) = explode('?', $avatar); - $default_url = $theme_url . $default_url; - # you get back the absoulte url to default in the avatar url? - $this->assertEquals($params, "d=$default_url&s=32"); - } - # you get back url? - $this->assertTrue(substr($theme_url . $avatar, 0, 5) == "http:"); + $this->assertTrue(substr($avatar, 0, 5) == "http:"); + } + + public function testGravatar() + { + if (!TestTimberImage::is_connected()) { + $this->markTestSkipped('Cannot test avatar images when not connected to internet'); } + $post_id = $this->factory->post->create(); + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + 'comment_author' => 'jarednova', + 'comment_author_email' => 'jarednova@upstatement.com', + ]); + $comment = Timber\Timber::get_comment($comment_id); + $gravatar = md5(file_get_contents($comment->avatar())); + /* this keeps changing b/c of compression tweaks on WP.org, disabling the test */ + //$this->assertEquals($gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg'))); + + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + 'comment_author' => 'jarednova', + 'comment_author_email' => 'notjared@upstatement.com', + ]); + $comment = Timber\Timber::get_comment($comment_id); + $not_gravatar = md5(file_get_contents($comment->avatar())); + $this->assertNotEquals($not_gravatar, md5(file_get_contents(dirname(__FILE__) . '/assets/jarednova.jpeg'))); + } - public function valid_avatar($comment, $default_url) - { - $avatar = $comment->avatar(32, $default_url); - if (strstr($avatar, '?')) { - list($url, $params) = explode('?', $avatar); - # you get back the default in the avatar url? - $this->assertEquals($params, "d=$default_url&s=32"); - } - # you get back url? - $this->assertTrue(substr($avatar, 0, 5) == "http:"); + public function testAvatarSimple() + { + if (!TestTimberImage::is_connected()) { + $this->markTestSkipped('Cannot test avatar images when not connected to internet'); + } + $theme_url = get_theme_root_uri() . '/' . get_stylesheet(); + $post_id = $this->factory->post->create(); + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + ]); + $comment = Timber\Timber::get_comment($comment_id); + + # test default gravatr holding image + $avatar = $comment->avatar(32, "mystery"); + + $this->assertTrue(substr($avatar, 0, 5) == "http:"); + + # does it work if its SSL? + $_SERVER['HTTPS'] = 'on'; + $avatar = $comment->avatar(32, "mystery"); + $this->assertTrue(200 === $this->crawl($avatar)); + $this->assertTrue(substr($avatar, 0, 6) == "https:"); + $_SERVER['HTTPS'] = 'off'; + + # pass custom url on different domain. can't check by crawling as + # i get a 302 regardless of default url + # so just check it comes back with it in the url + $this->valid_avatar($comment, "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png"); + + # same domain. + $this->valid_avatar($comment, $theme_url . "/images/default.png"); + + #relative + $default_url = "/images/default.png"; + $avatar = $comment->avatar(32, $default_url); + if (strstr($avatar, '?')) { + list($url, $params) = explode('?', $avatar); + $default_url = $theme_url . $default_url; + # you get back the absoulte url to default in the avatar url? + $this->assertEquals($params, "d=$default_url&s=32"); } + # you get back url? + $this->assertTrue(substr($theme_url . $avatar, 0, 5) == "http:"); + } - public function crawl($url) - { - $handle = curl_init($url); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - /* Get the HTML or whatever is linked in $url. */ - $response = curl_exec($handle); - /* Check for 404 (file not found). */ - $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); - curl_close($handle); - return $httpCode; + public function valid_avatar($comment, $default_url) + { + $avatar = $comment->avatar(32, $default_url); + if (strstr($avatar, '?')) { + list($url, $params) = explode('?', $avatar); + # you get back the default in the avatar url? + $this->assertEquals($params, "d=$default_url&s=32"); } + # you get back url? + $this->assertTrue(substr($avatar, 0, 5) == "http:"); + } + + public function crawl($url) + { + $handle = curl_init($url); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); + /* Get the HTML or whatever is linked in $url. */ + $response = curl_exec($handle); + /* Check for 404 (file not found). */ + $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); + curl_close($handle); + return $httpCode; } +} diff --git a/tests/test-timber-comment-thread.php b/tests/test-timber-comment-thread.php index 0498a65c2..11484f3f0 100644 --- a/tests/test-timber-comment-thread.php +++ b/tests/test-timber-comment-thread.php @@ -1,63 +1,63 @@ factory->post->create([ - 'post_title' => 'Gobbles', - ]); - $comment_id_array = $this->factory->comment->create_many(5, [ - 'comment_post_ID' => $post_id, - ]); - $args = []; - $ct = new Timber\CommentThread($post_id, $args); - $this->assertEquals(5, count($ct)); - } + $post_id = $this->factory->post->create([ + 'post_title' => 'Gobbles', + ]); + $comment_id_array = $this->factory->comment->create_many(5, [ + 'comment_post_ID' => $post_id, + ]); + $args = []; + $ct = new Timber\CommentThread($post_id, $args); + $this->assertEquals(5, count($ct)); + } - public function testCommentThreadCountMethod() - { - $post_id = $this->factory->post->create([ - 'post_title' => 'Gobbles', - ]); - $comment_id_array = $this->factory->comment->create_many(5, [ - 'comment_post_ID' => $post_id, - ]); - $args = []; - $ct = new Timber\CommentThread($post_id, $args); - $this->assertEquals(5, $ct->count()); - } + public function testCommentThreadCountMethod() + { + $post_id = $this->factory->post->create([ + 'post_title' => 'Gobbles', + ]); + $comment_id_array = $this->factory->comment->create_many(5, [ + 'comment_post_ID' => $post_id, + ]); + $args = []; + $ct = new Timber\CommentThread($post_id, $args); + $this->assertEquals(5, $ct->count()); + } + + public function testShowUnmoderatedCommentIfByAnon() + { + global $wp_version; + $post_id = $this->factory->post->create(); - public function testShowUnmoderatedCommentIfByAnon() - { - global $wp_version; - $post_id = $this->factory->post->create(); + $quote = "And in that moment, I was a marine biologist"; + $comment_id = $this->factory->comment->create([ + 'comment_post_ID' => $post_id, + 'comment_content' => $quote, + 'comment_approved' => 0, + 'comment_author_email' => 'jarednova@upstatement.com', + ]); - $quote = "And in that moment, I was a marine biologist"; - $comment_id = $this->factory->comment->create([ - 'comment_post_ID' => $post_id, - 'comment_content' => $quote, - 'comment_approved' => 0, - 'comment_author_email' => 'jarednova@upstatement.com', - ]); + $comment = get_comment($comment_id); - $comment = get_comment($comment_id); + $post = Timber::get_post($post_id); + $this->assertEquals(0, count($post->comments())); - $post = Timber::get_post($post_id); + $_GET['unapproved'] = $comment->comment_ID; + $_GET['moderation-hash'] = wp_hash($comment->comment_date_gmt); + $post = Timber::get_post($post_id); + if (!function_exists('wp_get_unapproved_comment_author_email')) { $this->assertEquals(0, count($post->comments())); - - $_GET['unapproved'] = $comment->comment_ID; - $_GET['moderation-hash'] = wp_hash($comment->comment_date_gmt); - $post = Timber::get_post($post_id); - if (!function_exists('wp_get_unapproved_comment_author_email')) { - $this->assertEquals(0, count($post->comments())); - } else { - $timber_comment = $post->comments()[0]; - $this->assertEquals($quote, $timber_comment->comment_content); - } + } else { + $timber_comment = $post->comments()[0]; + $this->assertEquals($quote, $timber_comment->comment_content); } } +} diff --git a/tests/test-timber-debug.php b/tests/test-timber-debug.php index f8f7f4eeb..49c8aca8c 100644 --- a/tests/test-timber-debug.php +++ b/tests/test-timber-debug.php @@ -1,6 +1,5 @@ markTestSkipped('It belongs to the ages now'); - } + $this->markTestSkipped('It belongs to the ages now'); + } - public function testGetPostsByMeta() - { - $this->markTestSkipped('It belongs to the ages now'); - } + public function testGetPostsByMeta() + { + $this->markTestSkipped('It belongs to the ages now'); + } - public function testTwitterify() - { - $this->markTestSkipped('It belongs to the ages now'); - } + public function testTwitterify() + { + $this->markTestSkipped('It belongs to the ages now'); } +} diff --git a/tests/test-timber-helper.php b/tests/test-timber-helper.php index 61ed491a6..57449d1a4 100644 --- a/tests/test-timber-helper.php +++ b/tests/test-timber-helper.php @@ -2,390 +2,389 @@ require_once(__DIR__ . '/php/timber-post-subclass.php'); +/** + * @group posts-api + * @group terms-api + * @group users-api + */ +class TestTimberHelper extends Timber_UnitTestCase +{ + public function testPluckArray() + { + $arr = []; + $arr[] = [ + 'name' => 'Bill', + 'number' => 42, + ]; + $arr[] = [ + 'name' => 'Barack', + 'number' => 44, + ]; + $arr[] = [ + 'name' => 'Hillary', + 'number' => 45, + ]; + $names = \Timber\Helper::pluck($arr, 'name'); + $this->assertEquals(['Bill', 'Barack', 'Hillary'], $names); + } + + public function testPluckArrayMissing() + { + $arr = []; + $arr[] = [ + 'name' => 'Bill', + 'number' => 42, + ]; + $arr[] = [ + 'name' => 'Barack', + 'number' => 44, + ]; + $arr[] = [ + 'name' => 'Hillary', + 'number' => 45, + ]; + $arr[] = [ + 'name' => 'Donald', + ]; + $names = \Timber\Helper::pluck($arr, 'number'); + $this->assertEquals([42, 44, 45], $names); + } + + public function testPluckObject() + { + $billy = new stdClass(); + $billy->name = 'Billy Corgan'; + $billy->instrument = 'guitar'; + $jimmy = new stdClass(); + $jimmy->name = 'Jimmy Chamberlin'; + $jimmy->instrument = 'drums'; + $pumpkins = [$billy, $jimmy]; + $instruments = \Timber\Helper::pluck($pumpkins, 'instrument'); + $this->assertEquals(['guitar', 'drums'], $instruments); + } + + public function testPluckObjectWithMethod() + { + $this->register_post_classmap_temporarily([ + 'post' => TimberPostSubclass::class, + ]); + + $tps = Timber::get_post($this->factory->post->create()); + $jimmy = new stdClass(); + $jimmy->name = 'Jimmy'; + $pumpkins = [$tps, $jimmy]; + $bar = \Timber\Helper::pluck($pumpkins, 'foo'); + $this->assertEquals(['bar'], $bar); + } + + public function testTrimCharacters() + { + $text = "Sometimes you need to do such weird things like remove all comments from your project."; + $trimmed = \Timber\TextHelper::trim_characters($text, 20); + $this->assertEquals("Sometimes yo…", $trimmed); + } + + public function testCloseTagsWithSelfClosingTags() + { + $p = '
My thing is this
My thing is this
My thing is this
My thing is this
newest!
response
Respond2Respond
oldest!
newest!
response
Respond2Respond
oldest!
Heres the start to a thing
@@ -77,11 +77,11 @@ public function testGutenbergExcerptOption()Heres the read more stuff that we shant see!
'; - $post_id = $this->factory->post->create([ - 'post_content' => $content_1, - ]); - $post = Timber::get_post($post_id); + $post_id = $this->factory->post->create([ + 'post_content' => $content_1, + ]); + $post = Timber::get_post($post_id); - $this->assertEquals('Heres the read more stuff that we shant see!
', trim($post->content())); - } + $this->assertEquals('Heres the read more stuff that we shant see!
', trim($post->content())); } +} diff --git a/tests/test-timber-post-convert.php b/tests/test-timber-post-convert.php index 4e45ad6bf..bdeb1a1d7 100644 --- a/tests/test-timber-post-convert.php +++ b/tests/test-timber-post-convert.php @@ -1,73 +1,73 @@ factory->post->create(); - $post = Timber::get_post($post_id); - $post_id = $this->factory->post->create([ - 'post_title' => 'Maybe Child Post', - ]); - $posts = get_posts([ - 'post__in' => [$post_id], - ]); - $converted = $post->convert($posts[0]); - $this->assertEquals($post_id, $converted->id); - $this->assertEquals('Timber\Post', get_class($converted)); - } + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $post_id = $this->factory->post->create([ + 'post_title' => 'Maybe Child Post', + ]); + $posts = get_posts([ + 'post__in' => [$post_id], + ]); + $converted = $post->convert($posts[0]); + $this->assertEquals($post_id, $converted->id); + $this->assertEquals('Timber\Post', get_class($converted)); + } - public function testConvertSingleItemArray() - { - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $post_id = $this->factory->post->create([ - 'post_title' => 'Maybe Child Post', - ]); - $posts = get_posts([ - 'post__in' => [$post_id], - ]); - $converted = $post->convert($posts); - $this->assertEquals($post_id, $converted[0]->id); - $this->assertEquals('Timber\Post', get_class($converted[0])); - } + public function testConvertSingleItemArray() + { + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $post_id = $this->factory->post->create([ + 'post_title' => 'Maybe Child Post', + ]); + $posts = get_posts([ + 'post__in' => [$post_id], + ]); + $converted = $post->convert($posts); + $this->assertEquals($post_id, $converted[0]->id); + $this->assertEquals('Timber\Post', get_class($converted[0])); + } - public function testConvertArray() - { - $post_ids = $this->factory->post->create_many(8, [ - 'post_title' => 'Sample Post ' . rand(1, 999), - ]); + public function testConvertArray() + { + $post_ids = $this->factory->post->create_many(8, [ + 'post_title' => 'Sample Post ' . rand(1, 999), + ]); - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $posts = get_posts([ - 'post__in' => $post_ids, - 'orderby' => 'post__in', - ]); - $converted = $post->convert($posts); - $this->assertEquals($post_ids[2], $converted[2]->id); - $this->assertEquals('Timber\Post', get_class($converted[3])); - } + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $posts = get_posts([ + 'post__in' => $post_ids, + 'orderby' => 'post__in', + ]); + $converted = $post->convert($posts); + $this->assertEquals($post_ids[2], $converted[2]->id); + $this->assertEquals('Timber\Post', get_class($converted[3])); + } - public function testNestedArray() - { - $post_ids = $this->factory->post->create_many(8, [ - 'post_title' => 'Sample Post ' . rand(1, 999), - ]); + public function testNestedArray() + { + $post_ids = $this->factory->post->create_many(8, [ + 'post_title' => 'Sample Post ' . rand(1, 999), + ]); - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $posts = get_posts([ - 'post__in' => $post_ids, - 'orderby' => 'post__in', - ]); - $arr = [$post, $posts]; + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $posts = get_posts([ + 'post__in' => $post_ids, + 'orderby' => 'post__in', + ]); + $arr = [$post, $posts]; - $converted = $post->convert($arr); - $this->assertEquals($post_ids[2], $converted[1][2]->id); - $this->assertEquals('Timber\Post', get_class($converted[1][3])); - } + $converted = $post->convert($arr); + $this->assertEquals($post_ids[2], $converted[1][2]->id); + $this->assertEquals('Timber\Post', get_class($converted[1][3])); } +} diff --git a/tests/test-timber-post-excerpt-object.php b/tests/test-timber-post-excerpt-object.php index 726424075..67240b5f5 100644 --- a/tests/test-timber-post-excerpt-object.php +++ b/tests/test-timber-post-excerpt-object.php @@ -1,418 +1,418 @@ Govenment:Lincoln', + 'post_excerpt' => false, + ]); + $post = Timber::get_post($post_id); + $template = "{{ post.excerpt( {strip:'
Lincoln Government:
'}) }}";
+ $str = Timber::compile_string($template, [
+ 'post' => $post,
+ ]);
+ $this->assertEquals($expected . '
';
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => $expected,
+ 'post_content' => $this->gettysburg,
+ ]);
+ $post = Timber::get_post($post_id);
+ $template = '{{ post.excerpt({
+ strip: "
… Read More', $str);
+ }
+
+ public function testExcerptConstructorWithWords()
+ {
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => $this->gettysburg,
+ ]);
+ $post = Timber::get_post($post_id);
+ $excerpt = $post->excerpt([
+ 'words' => 4,
+ 'force' => true,
+ 'read_more' => false,
+ ]);
+
+ // Coerce excerpt to a string and test it.
+ $this->assertEquals('Four score and seven…', '' . $excerpt);
+ }
+
+ public function testExcerptConstructorWithChars()
+ {
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => $this->gettysburg,
+ ]);
+ $post = Timber::get_post($post_id);
+ $excerpt = $post->excerpt([
+ 'chars' => 20,
+ 'force' => true,
+ 'read_more' => false,
+ ]);
+
+ // Coerce excerpt to a string and test it.
+ $this->assertEquals('Four score and seven…', '' . $excerpt);
+ }
+
+ public function testExcerptConstructorWithEnd()
+ {
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => $this->gettysburg,
+ ]);
+ $post = Timber::get_post($post_id);
+ $excerpt = $post->excerpt([
+ 'chars' => 20,
+ 'force' => true,
+ 'read_more' => false,
+ 'end' => ' - kthxbi',
+ ]);
+
+ // Coerce excerpt to a string and test it.
+ $this->assertEquals('Four score and seven - kthxbi', '' . $excerpt);
+ }
+
+ public function testExcerptConstructorWithHtml()
+ {
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => 'Yo'
+ . ' CLICK'
+ . ' STRONG '
+ . $this->gettysburg,
+ ]);
+ $post = Timber::get_post($post_id);
+ $excerpt = $post->excerpt([
+ 'chars' => 26,
+ 'read_more' => false,
+ 'force' => true,
+ ]);
+
+ // Coerce excerpt to a string and test it.
+ $this->assertEquals('Yo CLICK STRONG Four score…', '' . $excerpt);
+ }
+
+ public function testExcerptConstructorStrippingSomeTags()
+ {
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => 'Yo'
+ . ' CLICK'
+ . ' STRONG '
+ . $this->gettysburg,
+ ]);
+ $post = Timber::get_post($post_id);
+ $excerpt = $post->excerpt([
+ 'words' => 5,
+ 'read_more' => false,
+ 'force' => true,
+ 'strip' => '',
+ ]);
+
+ // Coerce excerpt to a string and test it.
+ $this->assertEquals(
+ 'Yo CLICK STRONG Four…',
+ '' . $excerpt
+ );
+ }
+
+ public function testExcerptConstructorWithReadMore()
+ {
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => $this->gettysburg,
+ ]);
+ $post = Timber::get_post($post_id);
+ $readmore = 'read more! if you dare...';
+ $excerpt = $post->excerpt([
+ 'chars' => 20,
+ 'force' => true,
+ 'read_more' => 'read more! if you dare...',
+ ]);
+
+ $expected = sprintf(
+ 'Four score and seven… %s',
+ $post->link(),
+ $readmore
+ );
+
+ // Coerce excerpt to a string and test it.
+ $this->assertEquals($expected, '' . $excerpt);
+ }
+
+ public function testExcerptWithStyleTags()
+ {
+ global $wpdb;
+ $style = 'Yo. ';
+ $id = $wpdb->insert(
+ $wpdb->posts,
+ [
+ 'post_author' => '1',
+ 'post_content' => $style . $this->gettysburg,
+ 'post_title' => 'Thing',
+ 'post_date' => '2017-03-01 00:21:40',
+ 'post_date_gmt' => '2017-03-01 00:21:40',
+ ]
+ );
+ $post_id = $wpdb->insert_id;
+ $post = Timber::get_post($post_id);
+ $template = '{{ post.excerpt.length(9).read_more(false).strip(true) }}';
+ $str = Timber::compile_string($template, [
+ 'post' => $post,
+ ]);
+ $this->assertEquals('Yo. Four score and seven years ago our fathers…', $str);
+ }
+
+ public function testExcerptTags()
+ {
+ $post_id = $this->factory->post->create([
+ 'post_excerpt' => 'It turned out that just about anyone in authority — cops, judges, city leaders — was in on the game.',
+ ]);
+ $post = Timber::get_post($post_id);
+ $template = '{{ post.excerpt.length(3).read_more(false).strip(false) }}';
+ $str = Timber::compile_string($template, [
+ 'post' => $post,
+ ]);
+ $this->assertStringNotContainsString('
Lauren is a duck, but a great duck let me tell you why
', + ]); + $post = Timber::get_post($pid); + $template = '{{post.excerpt.strip(false)}}'; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals('Lauren is a duck, but a great duck let me tell you why Read More
', $str); + } + + public function testExcerptWithStripAndClosingPTagForced() + { + $pid = $this->factory->post->create([ + 'post_excerpt' => 'Lauren is a duck, but a great duck let me tell you why
', + ]); + $post = Timber::get_post($pid); + $template = '{{post.excerpt.strip(false).force(4)}}'; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals('Lauren is a duck, but a great duck let me tell you why… Read More
', $str); + } + + public function testEmptyExcerpt() + { + $pid = $this->factory->post->create([ + 'post_excerpt' => '', + 'post_content' => '', + ]); + $post = Timber::get_post($pid); + $template = '{{ post.excerpt }}'; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals('', $str); + } + /** - * @group posts-api + * @ticket #2045 */ - class TestTimberPostExcerptObject extends Timber_UnitTestCase + public function testPageExcerptOnSearch() { - protected $gettysburg = 'Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.'; - - public function test1886Error() - { - $expected = 'Govenment:
Lincoln', - 'post_excerpt' => false, - ]); - $post = Timber::get_post($post_id); - $template = "{{ post.excerpt( {strip:'
Lincoln Government:
'}) }}";
- $str = Timber::compile_string($template, [
- 'post' => $post,
- ]);
- $this->assertEquals($expected . '
';
- $post_id = $this->factory->post->create([
- 'post_excerpt' => $expected,
- 'post_content' => $this->gettysburg,
- ]);
- $post = Timber::get_post($post_id);
- $template = '{{ post.excerpt({
- strip: "
… Read More', $str);
- }
-
- public function testExcerptConstructorWithWords()
- {
- $post_id = $this->factory->post->create([
- 'post_excerpt' => $this->gettysburg,
- ]);
- $post = Timber::get_post($post_id);
- $excerpt = $post->excerpt([
- 'words' => 4,
- 'force' => true,
- 'read_more' => false,
- ]);
-
- // Coerce excerpt to a string and test it.
- $this->assertEquals('Four score and seven…', '' . $excerpt);
- }
-
- public function testExcerptConstructorWithChars()
- {
- $post_id = $this->factory->post->create([
- 'post_excerpt' => $this->gettysburg,
- ]);
- $post = Timber::get_post($post_id);
- $excerpt = $post->excerpt([
- 'chars' => 20,
- 'force' => true,
- 'read_more' => false,
- ]);
-
- // Coerce excerpt to a string and test it.
- $this->assertEquals('Four score and seven…', '' . $excerpt);
- }
-
- public function testExcerptConstructorWithEnd()
- {
- $post_id = $this->factory->post->create([
- 'post_excerpt' => $this->gettysburg,
- ]);
- $post = Timber::get_post($post_id);
- $excerpt = $post->excerpt([
- 'chars' => 20,
- 'force' => true,
- 'read_more' => false,
- 'end' => ' - kthxbi',
- ]);
-
- // Coerce excerpt to a string and test it.
- $this->assertEquals('Four score and seven - kthxbi', '' . $excerpt);
- }
-
- public function testExcerptConstructorWithHtml()
- {
- $post_id = $this->factory->post->create([
- 'post_excerpt' => 'Yo'
- . ' CLICK'
- . ' STRONG '
- . $this->gettysburg,
- ]);
- $post = Timber::get_post($post_id);
- $excerpt = $post->excerpt([
- 'chars' => 26,
- 'read_more' => false,
- 'force' => true,
- ]);
-
- // Coerce excerpt to a string and test it.
- $this->assertEquals('Yo CLICK STRONG Four score…', '' . $excerpt);
- }
-
- public function testExcerptConstructorStrippingSomeTags()
- {
- $post_id = $this->factory->post->create([
- 'post_excerpt' => 'Yo'
- . ' CLICK'
- . ' STRONG '
- . $this->gettysburg,
- ]);
- $post = Timber::get_post($post_id);
- $excerpt = $post->excerpt([
- 'words' => 5,
- 'read_more' => false,
- 'force' => true,
- 'strip' => '',
- ]);
-
- // Coerce excerpt to a string and test it.
- $this->assertEquals(
- 'Yo CLICK STRONG Four…',
- '' . $excerpt
- );
- }
-
- public function testExcerptConstructorWithReadMore()
- {
- $post_id = $this->factory->post->create([
- 'post_excerpt' => $this->gettysburg,
- ]);
- $post = Timber::get_post($post_id);
- $readmore = 'read more! if you dare...';
- $excerpt = $post->excerpt([
- 'chars' => 20,
- 'force' => true,
- 'read_more' => 'read more! if you dare...',
- ]);
-
- $expected = sprintf(
- 'Four score and seven… %s',
- $post->link(),
- $readmore
- );
-
- // Coerce excerpt to a string and test it.
- $this->assertEquals($expected, '' . $excerpt);
- }
-
- public function testExcerptWithStyleTags()
- {
- global $wpdb;
- $style = 'Yo. ';
- $id = $wpdb->insert(
- $wpdb->posts,
- [
- 'post_author' => '1',
- 'post_content' => $style . $this->gettysburg,
- 'post_title' => 'Thing',
- 'post_date' => '2017-03-01 00:21:40',
- 'post_date_gmt' => '2017-03-01 00:21:40',
- ]
- );
- $post_id = $wpdb->insert_id;
- $post = Timber::get_post($post_id);
- $template = '{{ post.excerpt.length(9).read_more(false).strip(true) }}';
- $str = Timber::compile_string($template, [
- 'post' => $post,
- ]);
- $this->assertEquals('Yo. Four score and seven years ago our fathers…', $str);
- }
-
- public function testExcerptTags()
- {
- $post_id = $this->factory->post->create([
- 'post_excerpt' => 'It turned out that just about anyone in authority — cops, judges, city leaders — was in on the game.',
- ]);
- $post = Timber::get_post($post_id);
- $template = '{{ post.excerpt.length(3).read_more(false).strip(false) }}';
- $str = Timber::compile_string($template, [
- 'post' => $post,
- ]);
- $this->assertStringNotContainsString('
Lauren is a duck, but a great duck let me tell you why
', - ]); - $post = Timber::get_post($pid); - $template = '{{post.excerpt.strip(false)}}'; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals('Lauren is a duck, but a great duck let me tell you why Read More
', $str); - } - - public function testExcerptWithStripAndClosingPTagForced() - { - $pid = $this->factory->post->create([ - 'post_excerpt' => 'Lauren is a duck, but a great duck let me tell you why
', - ]); - $post = Timber::get_post($pid); - $template = '{{post.excerpt.strip(false).force(4)}}'; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals('Lauren is a duck, but a great duck let me tell you why… Read More
', $str); - } - - public function testEmptyExcerpt() - { - $pid = $this->factory->post->create([ - 'post_excerpt' => '', - 'post_content' => '', - ]); - $post = Timber::get_post($pid); - $template = '{{ post.excerpt }}'; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals('', $str); - } - - /** - * @ticket #2045 - */ - public function testPageExcerptOnSearch() - { - $pid = $this->factory->post->create([ - 'post_type' => 'page', - 'post_content' => 'What a beautiful day for a ballgame!', - 'post_excerpt' => '', - ]); - $post = Timber::get_post($pid); - $template = '{{ post.excerpt }}'; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals( - 'What a beautiful day for a ballgame!', - $str - ); - } + $pid = $this->factory->post->create([ + 'post_type' => 'page', + 'post_content' => 'What a beautiful day for a ballgame!', + 'post_excerpt' => '', + ]); + $post = Timber::get_post($pid); + $template = '{{ post.excerpt }}'; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals( + 'What a beautiful day for a ballgame!', + $str + ); } +} diff --git a/tests/test-timber-post-password.php b/tests/test-timber-post-password.php index 10fec7632..82f0288bf 100644 --- a/tests/test-timber-post-password.php +++ b/tests/test-timber-post-password.php @@ -1,56 +1,56 @@ factory->post->create(); - $post = Timber::get_post($post_id); - $post->post_content = $quote; - $post->post_password = 'burrito'; - wp_update_post($post); - $password_form = get_the_password_form($post->ID); - $this->assertEquals(wpautop($quote), $post->content()); - } + $quote = 'The way to do well is to do well.'; + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $post->post_content = $quote; + $post->post_password = 'burrito'; + wp_update_post($post); + $password_form = get_the_password_form($post->ID); + $this->assertEquals(wpautop($quote), $post->content()); + } - public function testPasswordedContentWhenEnabled() - { - add_filter('timber/post/content/show_password_form_for_protected', function ($maybe_show) { - return true; - }); - $quote = 'The way to do well is to do well.'; - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $post->post_content = $quote; - $post->post_password = 'burrito'; - wp_update_post($post); - $password_form = get_the_password_form($post->ID); - $this->assertEquals($password_form, $post->content()); - } + public function testPasswordedContentWhenEnabled() + { + add_filter('timber/post/content/show_password_form_for_protected', function ($maybe_show) { + return true; + }); + $quote = 'The way to do well is to do well.'; + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $post->post_content = $quote; + $post->post_password = 'burrito'; + wp_update_post($post); + $password_form = get_the_password_form($post->ID); + $this->assertEquals($password_form, $post->content()); + } - public function testPasswordedContentWhenEnabledWithCustomForm() - { - add_filter('timber/post/content/show_password_form_for_protected', function ($maybe_show) { - return true; - }); - add_filter('timber/post/content/password_form', function ($form, $post) { - return Timber::compile('assets/password-form.twig', [ - 'post' => $post, - ]); - }, 10, 2); - $quote = 'The way to do well is to do well.'; - $post_id = $this->factory->post->create([ - 'post_title' => 'Secrets!', + public function testPasswordedContentWhenEnabledWithCustomForm() + { + add_filter('timber/post/content/show_password_form_for_protected', function ($maybe_show) { + return true; + }); + add_filter('timber/post/content/password_form', function ($form, $post) { + return Timber::compile('assets/password-form.twig', [ + 'post' => $post, ]); - $post = Timber::get_post($post_id); - $post->post_content = $quote; - $post->post_password = 'burrito'; - wp_update_post($post); - $password_form = ''; - $this->assertEquals($password_form, $post->content()); - } + }, 10, 2); + $quote = 'The way to do well is to do well.'; + $post_id = $this->factory->post->create([ + 'post_title' => 'Secrets!', + ]); + $post = Timber::get_post($post_id); + $post->post_content = $quote; + $post->post_password = 'burrito'; + wp_update_post($post); + $password_form = ''; + $this->assertEquals($password_form, $post->content()); } +} diff --git a/tests/test-timber-post-terms.php b/tests/test-timber-post-terms.php index 84f36b2a0..2662d1fc4 100644 --- a/tests/test-timber-post-terms.php +++ b/tests/test-timber-post-terms.php @@ -1,176 +1,176 @@ factory->post->create(); - $post = Timber::get_post($pid); - - // create a new tag and associate it with the post - $dummy_tag = wp_insert_term('whatever', 'post_tag'); - wp_set_object_terms($pid, $dummy_tag['term_id'], 'post_tag', true); - - $this->add_filter_temporarily('timber/term/classmap', function () { - return [ - 'post_tag' => MyTimberTerm::class, - ]; - }); - - $terms = $post->terms([ - 'query' => [ - 'taxonomy' => 'post_tag', - ], - ]); - $this->assertInstanceOf(MyTimberTerm::class, $terms[0]); - - $post = Timber::get_post($pid); - $terms = $post->terms([ - 'query' => [ - 'taxonomy' => 'post_tag', - ], - 'merge' => true, - ]); - $this->assertInstanceOf(MyTimberTerm::class, $terms[0]); - } - - /** - * @ticket #2203 - */ - public function testPostTermsUsingUsingFactories() - { - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); - // create a new tag and associate it with the post - $dummy_tag = wp_insert_term('whatever', 'post_tag'); - $dummy_cat = wp_insert_term('news', 'category'); - wp_set_object_terms($pid, $dummy_tag['term_id'], 'post_tag', true); + // create a new tag and associate it with the post + $dummy_tag = wp_insert_term('whatever', 'post_tag'); + wp_set_object_terms($pid, $dummy_tag['term_id'], 'post_tag', true); - wp_set_object_terms($pid, $dummy_cat['term_id'], 'category', true); + $this->add_filter_temporarily('timber/term/classmap', function () { + return [ + 'post_tag' => MyTimberTerm::class, + ]; + }); - $this->add_filter_temporarily('timber/term/classmap', function () { - return [ - 'post_tag' => MyTimberTerm::class, - ]; - }); - - $terms = $post->terms([ + $terms = $post->terms([ + 'query' => [ 'taxonomy' => 'post_tag', - ]); - $this->assertInstanceOf(MyTimberTerm::class, $terms[0]); - - $post = Timber::get_post($pid); - $terms = $post->terms([], [ - 'merge' => false, - ]); - $this->assertEquals('whatever', $terms['post_tag'][0]->name); - - $terms = $post->terms([], [ - 'merge' => true, - ]); - $this->assertEquals(3, count($terms)); - } + ], + ]); + $this->assertInstanceOf(MyTimberTerm::class, $terms[0]); - /** - * @ticket #2163 - * This test confirms that term ordering works when sent through the query parameter of - * arguments. - */ - public function testPostTermOrder() - { - $pid = $this->factory->post->create(); - register_taxonomy('cars', 'post'); - $cars[] = $this->factory->term->create([ - 'name' => 'Honda Civic', - 'taxonomy' => 'cars', - ]); - $cars[] = $this->factory->term->create([ - 'name' => 'Toyota Corolla', - 'taxonomy' => 'cars', - ]); - $cars[] = $this->factory->term->create([ - 'name' => 'Toyota Camry', - 'taxonomy' => 'cars', - ]); - $cars[] = $this->factory->term->create([ - 'name' => 'Dodge Intrepid', - 'taxonomy' => 'cars', - ]); - foreach ($cars as $tid) { - $car = Timber::get_term($tid); - } - wp_set_object_terms($pid, $cars, 'cars', false); - $post = Timber::get_post($pid); - $template = "{% for term_item in post.terms({query : {taxonomy: 'cars', orderby: 'term_id', order: 'ASC'}}) %}{{ term_item.name }} {% endfor %}"; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals('Honda Civic Toyota Corolla Toyota Camry Dodge Intrepid ', $str); - } + $post = Timber::get_post($pid); + $terms = $post->terms([ + 'query' => [ + 'taxonomy' => 'post_tag', + ], + 'merge' => true, + ]); + $this->assertInstanceOf(MyTimberTerm::class, $terms[0]); + } - /** - * This should return an error because the "dfasdf" taxonomy doesn't exist - * NOTE: In Timber 1.x this returned a WP_Error. - */ - public function testTermExceptions() - { - self::enable_error_log(false); - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $terms = $post->terms('dfasdf'); - $this->assertEmpty($terms); - self::enable_error_log(true); - } + /** + * @ticket #2203 + */ + public function testPostTermsUsingUsingFactories() + { + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + + // create a new tag and associate it with the post + $dummy_tag = wp_insert_term('whatever', 'post_tag'); + $dummy_cat = wp_insert_term('news', 'category'); + wp_set_object_terms($pid, $dummy_tag['term_id'], 'post_tag', true); + + wp_set_object_terms($pid, $dummy_cat['term_id'], 'category', true); + + $this->add_filter_temporarily('timber/term/classmap', function () { + return [ + 'post_tag' => MyTimberTerm::class, + ]; + }); + + $terms = $post->terms([ + 'taxonomy' => 'post_tag', + ]); + $this->assertInstanceOf(MyTimberTerm::class, $terms[0]); + + $post = Timber::get_post($pid); + $terms = $post->terms([], [ + 'merge' => false, + ]); + $this->assertEquals('whatever', $terms['post_tag'][0]->name); + + $terms = $post->terms([], [ + 'merge' => true, + ]); + $this->assertEquals(3, count($terms)); + } - /** - * This shouldn't return an error because the "foobar" taxonomy DOES exist - */ - public function testTermFromNonExistentTaxonomy() - { - self::enable_error_log(false); - register_taxonomy('foobar', 'post'); - $pid = $this->factory->post->create(); - $post = Timber::get_post($pid); - $terms = $post->terms('foobar'); - $this->assertEmpty($terms); - self::enable_error_log(true); + /** + * @ticket #2163 + * This test confirms that term ordering works when sent through the query parameter of + * arguments. + */ + public function testPostTermOrder() + { + $pid = $this->factory->post->create(); + register_taxonomy('cars', 'post'); + $cars[] = $this->factory->term->create([ + 'name' => 'Honda Civic', + 'taxonomy' => 'cars', + ]); + $cars[] = $this->factory->term->create([ + 'name' => 'Toyota Corolla', + 'taxonomy' => 'cars', + ]); + $cars[] = $this->factory->term->create([ + 'name' => 'Toyota Camry', + 'taxonomy' => 'cars', + ]); + $cars[] = $this->factory->term->create([ + 'name' => 'Dodge Intrepid', + 'taxonomy' => 'cars', + ]); + foreach ($cars as $tid) { + $car = Timber::get_term($tid); } + wp_set_object_terms($pid, $cars, 'cars', false); + $post = Timber::get_post($pid); + $template = "{% for term_item in post.terms({query : {taxonomy: 'cars', orderby: 'term_id', order: 'ASC'}}) %}{{ term_item.name }} {% endfor %}"; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals('Honda Civic Toyota Corolla Toyota Camry Dodge Intrepid ', $str); + } - public function testTermNotMerged() - { - $pid = $this->factory->post->create(); + /** + * This should return an error because the "dfasdf" taxonomy doesn't exist + * NOTE: In Timber 1.x this returned a WP_Error. + */ + public function testTermExceptions() + { + self::enable_error_log(false); + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $terms = $post->terms('dfasdf'); + $this->assertEmpty($terms); + self::enable_error_log(true); + } - // create a new tag and category and associate each with the post - $tag_id = $this->factory->term->create([ - 'name' => 'whatever', - 'taxonomy' => 'post_tag', - ]); - $cat_id = $this->factory->term->create([ - 'name' => 'thingy', - 'taxonomy' => 'category', - ]); - wp_set_object_terms($pid, $tag_id, 'post_tag', true); - wp_set_object_terms($pid, $cat_id, 'category', true); - - $post = Timber::get_post($pid); - $terms = $post->terms([ - 'query' => [ - 'taxonomy' => 'all', - ], - 'merge' => false, - ]); - - $this->assertEquals($terms['post_tag'][0]->name, 'whatever'); - $this->assertEquals($terms['category'][0]->name, 'thingy'); - } + /** + * This shouldn't return an error because the "foobar" taxonomy DOES exist + */ + public function testTermFromNonExistentTaxonomy() + { + self::enable_error_log(false); + register_taxonomy('foobar', 'post'); + $pid = $this->factory->post->create(); + $post = Timber::get_post($pid); + $terms = $post->terms('foobar'); + $this->assertEmpty($terms); + self::enable_error_log(true); } - class MyTimberTerm extends Timber\Term + public function testTermNotMerged() { + $pid = $this->factory->post->create(); + + // create a new tag and category and associate each with the post + $tag_id = $this->factory->term->create([ + 'name' => 'whatever', + 'taxonomy' => 'post_tag', + ]); + $cat_id = $this->factory->term->create([ + 'name' => 'thingy', + 'taxonomy' => 'category', + ]); + wp_set_object_terms($pid, $tag_id, 'post_tag', true); + wp_set_object_terms($pid, $cat_id, 'category', true); + + $post = Timber::get_post($pid); + $terms = $post->terms([ + 'query' => [ + 'taxonomy' => 'all', + ], + 'merge' => false, + ]); + + $this->assertEquals($terms['post_tag'][0]->name, 'whatever'); + $this->assertEquals($terms['category'][0]->name, 'thingy'); } +} + +class MyTimberTerm extends Timber\Term +{ +} diff --git a/tests/test-timber-post-title.php b/tests/test-timber-post-title.php index 8420532c5..138f46f1d 100644 --- a/tests/test-timber-post-title.php +++ b/tests/test-timber-post-title.php @@ -1,17 +1,17 @@ factory->post->create([ - 'post_title' => 'Jared & Lauren', - ]); - $post = Timber::get_post($post_id); - $this->assertEquals(get_the_title($post_id), $post->title()); - $this->assertEquals(get_the_title($post_id), $post->post_title); - } + $post_id = $this->factory->post->create([ + 'post_title' => 'Jared & Lauren', + ]); + $post = Timber::get_post($post_id); + $this->assertEquals(get_the_title($post_id), $post->title()); + $this->assertEquals(get_the_title($post_id), $post->post_title); } +} diff --git a/tests/test-timber-post-type.php b/tests/test-timber-post-type.php index 696159a27..be2c4be5c 100644 --- a/tests/test-timber-post-type.php +++ b/tests/test-timber-post-type.php @@ -1,64 +1,64 @@ assertEquals('Posts', $obj->labels->name); - } + restore_current_locale(); + $obj = get_post_type_object('post'); + $this->assertEquals('Posts', $obj->labels->name); + } - public function testPostTypeProperty() - { - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $this->assertEquals('post', $post->post_type); - } + public function testPostTypeProperty() + { + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $this->assertEquals('post', $post->post_type); + } - /** - * @ticket #2111 - */ - public function testNonExistentPostType() - { - $post_type = new Timber\PostType('foobar'); - $this->assertEquals('foobar', $post_type); - $this->assertEquals('Timber\PostType', get_class($post_type)); - } + /** + * @ticket #2111 + */ + public function testNonExistentPostType() + { + $post_type = new Timber\PostType('foobar'); + $this->assertEquals('foobar', $post_type); + $this->assertEquals('Timber\PostType', get_class($post_type)); + } - public function testPostTypeMethodInTwig() - { - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $template = '{{post.post_type}}'; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals('post', $str); - } + public function testPostTypeMethodInTwig() + { + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $template = '{{post.post_type}}'; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals('post', $str); + } - public function testTypeMethodInTwig() - { - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $template = '{{post.type}}'; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals('post', $str); - } + public function testTypeMethodInTwig() + { + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $template = '{{post.type}}'; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals('post', $str); + } - public function testTypeMethodInTwigLabels() - { - $post_id = $this->factory->post->create(); - $post = Timber::get_post($post_id); - $template = '{{post.type.labels.name}}'; - $str = Timber::compile_string($template, [ - 'post' => $post, - ]); - $this->assertEquals('Posts', $str); - } + public function testTypeMethodInTwigLabels() + { + $post_id = $this->factory->post->create(); + $post = Timber::get_post($post_id); + $template = '{{post.type.labels.name}}'; + $str = Timber::compile_string($template, [ + 'post' => $post, + ]); + $this->assertEquals('Posts', $str); } +} diff --git a/tests/test-timber-revisions.php b/tests/test-timber-revisions.php index 579abc057..51834e200 100644 --- a/tests/test-timber-revisions.php +++ b/tests/test-timber-revisions.php @@ -1,366 +1,366 @@ queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - } - - public function testParentOfPost() - { - // Register Custom Post Type - - $args = [ - 'label' => __('Box', 'text_domain'), - 'description' => __('Post Type Description', 'text_domain'), - 'supports' => ['title', 'editor', 'revisions'], - 'taxonomies' => ['category', 'post_tag'], - 'hierarchical' => true, - 'public' => true, - 'show_ui' => true, - 'show_in_menu' => true, - 'menu_position' => 5, - 'show_in_admin_bar' => true, - 'show_in_nav_menus' => true, - 'can_export' => true, - 'has_archive' => true, - 'exclude_from_search' => false, - 'publicly_queryable' => true, - 'capability_type' => 'page', - ]; - register_post_type('box', $args); - - global $current_user; - global $wp_query; - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - $user->add_role('administrator'); - - $parent_id = $this->factory->post->create([ - 'post_content' => 'I am parent', - 'post_type' => 'box', - 'post_author' => $uid, - ]); - - $post_id = $this->factory->post->create([ - 'post_content' => 'I am child', - 'post_type' => 'box', - 'post_author' => $uid, - 'post_parent' => $parent_id, - ]); - - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - 'post_content' => 'I am revised', - ]); - - $post = Timber::get_post($post_id); - $parent = Timber::get_post($parent_id); - - //$this->assertEquals($parent_id, $post->parent()->id); - - self::setRevision($post_id); - $revision = Timber::get_post(); - - $this->assertEquals('I am revised', trim(strip_tags($revision->content()))); - - $revision_parent = $revision->parent(); - $this->assertEquals($parent_id, $revision_parent->id); - $this->assertEquals('I am parent', trim(strip_tags($revision_parent->content()))); - } - - public function testPreviewClass() - { - global $current_user; - global $wp_query; - - $quote = 'The way to do well is to do well.'; - $post_id = $this->factory->post->create([ - 'post_content' => $quote, - 'post_author' => 5, - ]); - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - 'post_content' => $quote . 'Yes', - ]); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - - $original_post = Timber::get_post($post_id); - $user = wp_set_current_user($uid); - - $user->add_role('administrator'); - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - $post = Timber::get_post(); - $this->assertEquals($original_post->class(), $post->class()); - } - - public function testPreviewTitleWithID() - { - global $current_user; - global $wp_query; - - $post_id = $this->factory->post->create([ - 'post_title' => 'I call it banana bread', - 'post_author' => 5, - ]); - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - 'post_title' => 'I call it fromage', - ]); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - - $user->add_role('administrator'); - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - $post = Timber::get_post($post_id); - $this->assertEquals('I call it fromage', $post->title()); - } - - public function testPreviewContentWithID() - { - global $current_user; - global $wp_query; - - $quote = 'The way to do well is to do well.'; - $post_id = $this->factory->post->create([ - 'post_content' => $quote, - 'post_author' => 5, - ]); - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - 'post_content' => $quote . 'Yes', - ]); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - - $user->add_role('administrator'); - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - $post = Timber::get_post($post_id); - $this->assertEquals($quote . 'Yes', trim(strip_tags($post->content()))); - } - - public function testPreviewContent() - { - global $current_user; - global $wp_query; - - $quote = 'The way to do well is to do well.'; - $post_id = $this->factory->post->create([ - 'post_content' => $quote, - 'post_author' => 5, - ]); - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - 'post_content' => $quote . 'Yes', - ]); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - - $user->add_role('administrator'); - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - $post = Timber::get_post(); - $this->assertEquals($quote . 'Yes', trim(strip_tags($post->content()))); - } - - public function testMultiPreviewRevisions() - { - global $current_user; - global $wp_query; - - $quote = 'The way to do well is to do well.'; - $post_id = $this->factory->post->create([ - 'post_content' => $quote, - 'post_author' => 5, - ]); - $old_revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - 'post_content' => $quote . 'Yes', - ]); - - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - 'post_content' => 'I am the one', - ]); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - - $user->add_role('administrator'); - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - $post = Timber::get_post(); - $this->assertEquals('I am the one', trim(strip_tags($post->content()))); - } - - public function testCustomFieldPreviewRevisionMethod() - { - global $current_user; - global $wp_query; - - $post_id = $this->factory->post->create([ - 'post_author' => 5, - ]); - update_field('test_field', 'The custom field content', $post_id); - - $assertCustomFieldVal = 'This has been revised'; - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - $user->add_role('administrator'); - - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - $post = Timber::get_post($post_id); - $str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [ - 'post' => $post, - ]); - $this->assertEquals($assertCustomFieldVal, $str_getfield); - } - - public function testCustomFieldPreviewRevisionImported() - { - global $current_user; - global $wp_query; - - $post_id = $this->factory->post->create([ - 'post_author' => 5, - ]); - update_field('test_field', 'The custom field content', $post_id); - - $assertCustomFieldVal = 'This has been revised'; - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - $user->add_role('administrator'); - - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $_GET['preview'] = true; - $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); - $post = Timber::get_post($post_id); - $str_direct = Timber::compile_string('{{ post.meta("test_field") }}', [ - 'post' => $post, - ]); - $this->assertEquals($assertCustomFieldVal, $str_direct); - } - - public function testCustomFieldPreviewNotRevision() - { - global $current_user; - global $wp_query; - $original_content = 'The custom field content'; - - $post_id = $this->factory->post->create([ - 'post_author' => 5, - ]); - update_field('test_field', $original_content, $post_id); - - $assertCustomFieldVal = 'This has been revised'; - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - $user->add_role('administrator'); - - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $post = Timber::get_post($post_id); - - $str_direct = Timber::compile_string('{{post.test_field}}', [ - 'post' => $post, - ]); - $str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [ - 'post' => $post, - ]); - - $this->assertEquals($original_content, $str_direct); - $this->assertEquals($original_content, $str_getfield); - } + global $wp_query; + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); } + + public function testParentOfPost() + { + // Register Custom Post Type + + $args = [ + 'label' => __('Box', 'text_domain'), + 'description' => __('Post Type Description', 'text_domain'), + 'supports' => ['title', 'editor', 'revisions'], + 'taxonomies' => ['category', 'post_tag'], + 'hierarchical' => true, + 'public' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'menu_position' => 5, + 'show_in_admin_bar' => true, + 'show_in_nav_menus' => true, + 'can_export' => true, + 'has_archive' => true, + 'exclude_from_search' => false, + 'publicly_queryable' => true, + 'capability_type' => 'page', + ]; + register_post_type('box', $args); + + global $current_user; + global $wp_query; + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + $user->add_role('administrator'); + + $parent_id = $this->factory->post->create([ + 'post_content' => 'I am parent', + 'post_type' => 'box', + 'post_author' => $uid, + ]); + + $post_id = $this->factory->post->create([ + 'post_content' => 'I am child', + 'post_type' => 'box', + 'post_author' => $uid, + 'post_parent' => $parent_id, + ]); + + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + 'post_content' => 'I am revised', + ]); + + $post = Timber::get_post($post_id); + $parent = Timber::get_post($parent_id); + + //$this->assertEquals($parent_id, $post->parent()->id); + + self::setRevision($post_id); + $revision = Timber::get_post(); + + $this->assertEquals('I am revised', trim(strip_tags($revision->content()))); + + $revision_parent = $revision->parent(); + $this->assertEquals($parent_id, $revision_parent->id); + $this->assertEquals('I am parent', trim(strip_tags($revision_parent->content()))); + } + + public function testPreviewClass() + { + global $current_user; + global $wp_query; + + $quote = 'The way to do well is to do well.'; + $post_id = $this->factory->post->create([ + 'post_content' => $quote, + 'post_author' => 5, + ]); + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + 'post_content' => $quote . 'Yes', + ]); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + + $original_post = Timber::get_post($post_id); + $user = wp_set_current_user($uid); + + $user->add_role('administrator'); + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); + $post = Timber::get_post(); + $this->assertEquals($original_post->class(), $post->class()); + } + + public function testPreviewTitleWithID() + { + global $current_user; + global $wp_query; + + $post_id = $this->factory->post->create([ + 'post_title' => 'I call it banana bread', + 'post_author' => 5, + ]); + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + 'post_title' => 'I call it fromage', + ]); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + + $user->add_role('administrator'); + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); + $post = Timber::get_post($post_id); + $this->assertEquals('I call it fromage', $post->title()); + } + + public function testPreviewContentWithID() + { + global $current_user; + global $wp_query; + + $quote = 'The way to do well is to do well.'; + $post_id = $this->factory->post->create([ + 'post_content' => $quote, + 'post_author' => 5, + ]); + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + 'post_content' => $quote . 'Yes', + ]); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + + $user->add_role('administrator'); + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); + $post = Timber::get_post($post_id); + $this->assertEquals($quote . 'Yes', trim(strip_tags($post->content()))); + } + + public function testPreviewContent() + { + global $current_user; + global $wp_query; + + $quote = 'The way to do well is to do well.'; + $post_id = $this->factory->post->create([ + 'post_content' => $quote, + 'post_author' => 5, + ]); + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + 'post_content' => $quote . 'Yes', + ]); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + + $user->add_role('administrator'); + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); + $post = Timber::get_post(); + $this->assertEquals($quote . 'Yes', trim(strip_tags($post->content()))); + } + + public function testMultiPreviewRevisions() + { + global $current_user; + global $wp_query; + + $quote = 'The way to do well is to do well.'; + $post_id = $this->factory->post->create([ + 'post_content' => $quote, + 'post_author' => 5, + ]); + $old_revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + 'post_content' => $quote . 'Yes', + ]); + + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + 'post_content' => 'I am the one', + ]); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + + $user->add_role('administrator'); + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); + $post = Timber::get_post(); + $this->assertEquals('I am the one', trim(strip_tags($post->content()))); + } + + public function testCustomFieldPreviewRevisionMethod() + { + global $current_user; + global $wp_query; + + $post_id = $this->factory->post->create([ + 'post_author' => 5, + ]); + update_field('test_field', 'The custom field content', $post_id); + + $assertCustomFieldVal = 'This has been revised'; + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + ]); + update_field('test_field', $assertCustomFieldVal, $revision_id); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + $user->add_role('administrator'); + + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); + $post = Timber::get_post($post_id); + $str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [ + 'post' => $post, + ]); + $this->assertEquals($assertCustomFieldVal, $str_getfield); + } + + public function testCustomFieldPreviewRevisionImported() + { + global $current_user; + global $wp_query; + + $post_id = $this->factory->post->create([ + 'post_author' => 5, + ]); + update_field('test_field', 'The custom field content', $post_id); + + $assertCustomFieldVal = 'This has been revised'; + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + ]); + update_field('test_field', $assertCustomFieldVal, $revision_id); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + $user->add_role('administrator'); + + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $_GET['preview'] = true; + $_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id); + $post = Timber::get_post($post_id); + $str_direct = Timber::compile_string('{{ post.meta("test_field") }}', [ + 'post' => $post, + ]); + $this->assertEquals($assertCustomFieldVal, $str_direct); + } + + public function testCustomFieldPreviewNotRevision() + { + global $current_user; + global $wp_query; + $original_content = 'The custom field content'; + + $post_id = $this->factory->post->create([ + 'post_author' => 5, + ]); + update_field('test_field', $original_content, $post_id); + + $assertCustomFieldVal = 'This has been revised'; + $revision_id = $this->factory->post->create([ + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $post_id, + ]); + update_field('test_field', $assertCustomFieldVal, $revision_id); + + $uid = $this->factory->user->create([ + 'user_login' => 'timber', + 'user_pass' => 'timber', + ]); + $user = wp_set_current_user($uid); + $user->add_role('administrator'); + + $wp_query->queried_object_id = $post_id; + $wp_query->queried_object = get_post($post_id); + $post = Timber::get_post($post_id); + + $str_direct = Timber::compile_string('{{post.test_field}}', [ + 'post' => $post, + ]); + $str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [ + 'post' => $post, + ]); + + $this->assertEquals($original_content, $str_direct); + $this->assertEquals($original_content, $str_getfield); + } +} diff --git a/tests/test-timber-shortcodes.php b/tests/test-timber-shortcodes.php index 92965ad4a..202773e2c 100644 --- a/tests/test-timber-shortcodes.php +++ b/tests/test-timber-shortcodes.php @@ -1,34 +1,34 @@ assertEquals('hello timber foo', trim($return)); - } + add_shortcode('timber_shortcode', function ($text) { + return 'timber ' . $text[0]; + }); + $return = Timber::compile('assets/test-shortcodes.twig'); + $this->assertEquals('hello timber foo', trim($return)); + } - /** - * @ticket #2268 - */ - public function testCustomFieldShortcode() - { - add_shortcode('foobar', function ($atts) { - return 'barfoo'; - }); + /** + * @ticket #2268 + */ + public function testCustomFieldShortcode() + { + add_shortcode('foobar', function ($atts) { + return 'barfoo'; + }); - $post_id = $this->factory->post->create(); - update_post_meta($post_id, 'customfield', '[foobar]'); - $template = '{{ post.customfield | shortcodes }}'; + $post_id = $this->factory->post->create(); + update_post_meta($post_id, 'customfield', '[foobar]'); + $template = '{{ post.customfield | shortcodes }}'; - $post = Timber::get_post($post_id); - $compiled = Timber::compile_string($template, [ - 'post' => $post, - ]); + $post = Timber::get_post($post_id); + $compiled = Timber::compile_string($template, [ + 'post' => $post, + ]); - $this->assertEquals('barfoo', $compiled); - } + $this->assertEquals('barfoo', $compiled); } +} diff --git a/tests/test-timber-sidebar.php b/tests/test-timber-sidebar.php index 569ac33c9..4d02c05b8 100644 --- a/tests/test-timber-sidebar.php +++ b/tests/test-timber-sidebar.php @@ -1,31 +1,31 @@ factory->post->create([ - 'post_title' => 'Sidebar post content', - ]); - $sidebar_context = []; - $sidebar_context['post'] = Timber::get_post($sidebar_post); - $context['sidebar'] = Timber::get_sidebar('assets/sidebar.twig', $sidebar_context); - $result = Timber::compile('assets/main-w-sidebar.twig', $context); - $this->assertEquals('I am the main stuff'; - $template = '{{foo|pretags}}'; - $str = Timber::compile_string($template, [ - 'foo' => $data, - ]); - $this->assertEquals('thing
<h1>thing</h1>', $str); - } + public function testTimberPreTags() + { + $data = '
'; + $template = '{{foo|pretags}}'; + $str = Timber::compile_string($template, [ + 'foo' => $data, + ]); + $this->assertEquals('thing
<h1>thing</h1>', $str); + } - public function testTimberFilterString() - { - $data['arr'] = ['foo', 'foo']; - $str = Timber::compile_string('{{arr|join(" ")}}', $data); - $this->assertEquals('foo foo', trim($str)); - $data['arr'] = ['bar']; - $str = Timber::compile_string('{{arr|join}}', $data); - $this->assertEquals('bar', trim($str)); - $data['arr'] = ['foo', 'bar']; - $str = Timber::compile_string('{{arr|join(", ")}}', $data); - $this->assertEquals('foo, bar', trim($str)); - $data['arr'] = 6; - $str = Timber::compile_string('{{arr}}', $data); - $this->assertEquals('6', trim($str)); - } + public function testTimberFilterString() + { + $data['arr'] = ['foo', 'foo']; + $str = Timber::compile_string('{{arr|join(" ")}}', $data); + $this->assertEquals('foo foo', trim($str)); + $data['arr'] = ['bar']; + $str = Timber::compile_string('{{arr|join}}', $data); + $this->assertEquals('bar', trim($str)); + $data['arr'] = ['foo', 'bar']; + $str = Timber::compile_string('{{arr|join(", ")}}', $data); + $this->assertEquals('foo, bar', trim($str)); + $data['arr'] = 6; + $str = Timber::compile_string('{{arr}}', $data); + $this->assertEquals('6', trim($str)); + } - public function testTwigFilterList() - { - $data['authors'] = ['Tom', 'Rick', 'Harry', 'Mike']; - $str = Timber::compile_string("{{authors|list}}", $data); - $this->assertEquals('Tom, Rick, Harry and Mike', $str); - } + public function testTwigFilterList() + { + $data['authors'] = ['Tom', 'Rick', 'Harry', 'Mike']; + $str = Timber::compile_string("{{authors|list}}", $data); + $this->assertEquals('Tom, Rick, Harry and Mike', $str); + } - public function testTwigFilterListOxford() - { - $data['authors'] = ['Tom', 'Rick', 'Harry', 'Mike']; - $str = Timber::compile_string("{{authors|list(',', ', and')}}", $data); - $this->assertEquals('Tom, Rick, Harry, and Mike', $str); - } + public function testTwigFilterListOxford() + { + $data['authors'] = ['Tom', 'Rick', 'Harry', 'Mike']; + $str = Timber::compile_string("{{authors|list(',', ', and')}}", $data); + $this->assertEquals('Tom, Rick, Harry, and Mike', $str); } +} diff --git a/tests/test-timber-twig.php b/tests/test-timber-twig.php index 322d8ec09..f96ccf4b3 100644 --- a/tests/test-timber-twig.php +++ b/tests/test-timber-twig.php @@ -1,403 +1,403 @@ 'foo', - ]); - $this->assertEquals('I like foo and bar', $return); + $lang_dir = get_stylesheet_directory() . '/languages'; + if (file_exists($lang_dir . '/en_US.po')) { + unlink($lang_dir . '/en_US.po'); } - - public function testTranslate() - { - load_textdomain('timber-test', __DIR__ . '/languages/timber-test-en_US.mo'); - - $str = "I like {{ __('thingy', 'timber-test') }}"; - $return = Timber::compile_string($str, [ - 'foo' => 'foo', - ]); - $this->assertEquals('I like Cheesy Poofs', $return); - - $str = "I like {{ __('doobie', 'timber-test') }}"; - $return = Timber::compile_string($str, [ - 'foo' => 'foo', - ]); - $this->assertEquals('I like doobie', $return); + if (file_exists($lang_dir . '/en_US.mo')) { + unlink($lang_dir . '/en_US.mo'); } + } - public function testTranslateAndFormat() - { - load_textdomain('timber-test', __DIR__ . '/languages/timber-test-en_US.mo'); + public function testFormat() + { + $str = '{{ "I like %s and %s"|format(foo, "bar") }}'; + $return = Timber::compile_string($str, [ + 'foo' => 'foo', + ]); + $this->assertEquals('I like foo and bar', $return); + } - $str = "You like {{__('%s', 'timber-test')|format('thingy')}}"; - $return = Timber::compile_string($str); - $this->assertEquals('You like thingy', $return); - $str = "You like {{__('%s'|format('thingy'), 'timber-test')}}"; - $return = Timber::compile_string($str); - $this->assertEquals('You like Cheesy Poofs', $return); - } + public function testTranslate() + { + load_textdomain('timber-test', __DIR__ . '/languages/timber-test-en_US.mo'); + + $str = "I like {{ __('thingy', 'timber-test') }}"; + $return = Timber::compile_string($str, [ + 'foo' => 'foo', + ]); + $this->assertEquals('I like Cheesy Poofs', $return); + + $str = "I like {{ __('doobie', 'timber-test') }}"; + $return = Timber::compile_string($str, [ + 'foo' => 'foo', + ]); + $this->assertEquals('I like doobie', $return); + } - public function testDoAction() - { - global $action_tally; - global $php_unit; - $php_unit = $this; - $action_tally = []; + public function testTranslateAndFormat() + { + load_textdomain('timber-test', __DIR__ . '/languages/timber-test-en_US.mo'); + + $str = "You like {{__('%s', 'timber-test')|format('thingy')}}"; + $return = Timber::compile_string($str); + $this->assertEquals('You like thingy', $return); + $str = "You like {{__('%s'|format('thingy'), 'timber-test')}}"; + $return = Timber::compile_string($str); + $this->assertEquals('You like Cheesy Poofs', $return); + } - $my_action_foo = function () { - global $action_tally, $php_unit; - $php_unit->assertTrue(true); - $action_tally[] = 'my_action_foo'; + public function testDoAction() + { + global $action_tally; + global $php_unit; + $php_unit = $this; + $action_tally = []; - return 'foo'; - }; + $my_action_foo = function () { + global $action_tally, $php_unit; + $php_unit->assertTrue(true); + $action_tally[] = 'my_action_foo'; - $my_action_args = function ($bar) { - global $action_tally, $php_unit; - $php_unit->assertEquals('bar', $bar); - $action_tally[] = 'my_action_args'; + return 'foo'; + }; - return 'foo'; - }; + $my_action_args = function ($bar) { + global $action_tally, $php_unit; + $php_unit->assertEquals('bar', $bar); + $action_tally[] = 'my_action_args'; - $timber_compile_done = function () { - global $action_tally, $php_unit; + return 'foo'; + }; - $php_unit->assertContains('my_action_args', $action_tally); - $php_unit->assertContains('my_action_foo', $action_tally); - }; + $timber_compile_done = function () { + global $action_tally, $php_unit; - $this->add_action_temporarily('my_action_foo', $my_action_foo); - $this->add_action_temporarily('my_action_args', $my_action_args); - $this->add_action_temporarily('timber/compile/done', $timber_compile_done); + $php_unit->assertContains('my_action_args', $action_tally); + $php_unit->assertContains('my_action_foo', $action_tally); + }; - $str = Timber::compile('assets/test-do-action.twig'); - $str = trim($str); + $this->add_action_temporarily('my_action_foo', $my_action_foo); + $this->add_action_temporarily('my_action_args', $my_action_args); + $this->add_action_temporarily('timber/compile/done', $timber_compile_done); - $this->assertEquals('Stuff', $str); - } + $str = Timber::compile('assets/test-do-action.twig'); + $str = trim($str); - public function testWordPressPasswordFilters() - { - $post_id = $this->factory->post->create([ - 'post_title' => 'My Private Post', - 'post_password' => 'abc123', - ]); - $context = []; - add_filter('protected_title_format', function ($title) { - return 'Protected: ' . $title; - }); - $context['post'] = Timber::get_post($post_id); - if (post_password_required($post_id)) { - $this->assertTrue(true); - $str = Timber::compile('assets/test-wp-filters.twig', $context); - $this->assertEquals('Protected: My Private Post', trim($str)); - } else { - $this->assertTrue(false, 'Something wrong with the post password reqd'); - } - } + $this->assertEquals('Stuff', $str); + } - public function testToArrayWithString() - { - $thing = 'thing'; - $str = '{% for thing in things|array %}{{thing}}{% endfor %}'; - $this->assertEquals('thing', Timber::compile_string($str, [ - 'things' => $thing, - ])); + public function testWordPressPasswordFilters() + { + $post_id = $this->factory->post->create([ + 'post_title' => 'My Private Post', + 'post_password' => 'abc123', + ]); + $context = []; + add_filter('protected_title_format', function ($title) { + return 'Protected: ' . $title; + }); + $context['post'] = Timber::get_post($post_id); + if (post_password_required($post_id)) { + $this->assertTrue(true); + $str = Timber::compile('assets/test-wp-filters.twig', $context); + $this->assertEquals('Protected: My Private Post', trim($str)); + } else { + $this->assertTrue(false, 'Something wrong with the post password reqd'); } + } - public function testToArrayWithArray() - { - $thing = ['thing', 'thang']; - $str = '{% for thing in things|array %}{{thing}}{% endfor %}'; - $this->assertEquals('thingthang', Timber::compile_string($str, [ - 'things' => $thing, - ])); - } + public function testToArrayWithString() + { + $thing = 'thing'; + $str = '{% for thing in things|array %}{{thing}}{% endfor %}'; + $this->assertEquals('thing', Timber::compile_string($str, [ + 'things' => $thing, + ])); + } - public function testTwigString() - { - $str = 'Foo'; - $arr = ['Bar', 'Quack']; - $twig = '{{string|join}}x{{array|join("x")}}'; - $this->assertEquals('FooxBarxQuack', trim(Timber::compile_string($twig, [ - 'string' => $str, - 'array' => $arr, - ]))); - } + public function testToArrayWithArray() + { + $thing = ['thing', 'thang']; + $str = '{% for thing in things|array %}{{thing}}{% endfor %}'; + $this->assertEquals('thingthang', Timber::compile_string($str, [ + 'things' => $thing, + ])); + } - /** - * @expectedDeprecated {{ my_object | get_class }} - */ - public function testFilterFunction() - { - $pid = $this->factory->post->create([ - 'post_title' => 'Foo', - ]); - $post = Timber::get_post($pid); - $str = 'I am a {{post | get_class }}'; - $this->assertEquals('I am a Timber\Post', Timber::compile_string($str, [ - 'post' => $post, - ])); - } + public function testTwigString() + { + $str = 'Foo'; + $arr = ['Bar', 'Quack']; + $twig = '{{string|join}}x{{array|join("x")}}'; + $this->assertEquals('FooxBarxQuack', trim(Timber::compile_string($twig, [ + 'string' => $str, + 'array' => $arr, + ]))); + } - public function testFilterTruncate() - { - $gettysburg = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'; - $str = Timber::compile_string("{{address | truncate(6)}}", [ - 'address' => $gettysburg, - ]); - $this->assertEquals('Four score and seven years ago…', $str); - } + /** + * @expectedDeprecated {{ my_object | get_class }} + */ + public function testFilterFunction() + { + $pid = $this->factory->post->create([ + 'post_title' => 'Foo', + ]); + $post = Timber::get_post($pid); + $str = 'I am a {{post | get_class }}'; + $this->assertEquals('I am a Timber\Post', Timber::compile_string($str, [ + 'post' => $post, + ])); + } - public function testFilterTrimCharacters() - { - $gettysburg = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'; - $str = Timber::compile_string("{{content | excerpt_chars(100)}}", [ - 'content' => $gettysburg, - ]); - $this->assertEquals('Four score and seven years ago our fathers brought forth on this continent, a new nation, co…', $str); - } + public function testFilterTruncate() + { + $gettysburg = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'; + $str = Timber::compile_string("{{address | truncate(6)}}", [ + 'address' => $gettysburg, + ]); + $this->assertEquals('Four score and seven years ago…', $str); + } - public function testSetSimple() - { - $result = Timber::compile('assets/set-simple.twig', [ - 'foo' => 'bar', - ]); - $this->assertEquals('jiggy', trim($result)); - } + public function testFilterTrimCharacters() + { + $gettysburg = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'; + $str = Timber::compile_string("{{content | excerpt_chars(100)}}", [ + 'content' => $gettysburg, + ]); + $this->assertEquals('Four score and seven years ago our fathers brought forth on this continent, a new nation, co…', $str); + } - public function testEscUrl() - { - $url = 'http://example.com/Mr WordPress'; - $str = Timber::compile_string("{{the_url | e('esc_url')}}", [ - 'the_url' => $url, - ]); - $this->assertEquals('http://example.com/Mr%20WordPress', $str); - } + public function testSetSimple() + { + $result = Timber::compile('assets/set-simple.twig', [ + 'foo' => 'bar', + ]); + $this->assertEquals('jiggy', trim($result)); + } - public function testWpKsesPost() - { - $evil_script = '