function testAvatar() { 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(array('comment_post_ID' => $post_id)); $comment = new TimberComment($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, get_template_directory_uri() . "/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 = get_template_directory_uri() . $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(get_template_directory_uri() . $avatar, 0, 5) == "http:"); }
function testTimberImageInTwigToString() { $iid = TestTimberImage::get_image_attachment(); $str = '{{TimberImage(' . $iid . ')}}'; $compiled = Timber::compile_string($str); $this->assertEquals('http://example.org/wp-content/uploads/' . date('Y') . '/' . date('m') . '/arch.jpg', $compiled); }
function testSiteIcon() { $icon_id = TestTimberImage::get_image_attachment(0, 'cardinals.jpg'); update_option('site_icon', $icon_id); $site = new TimberSite(); $icon = $site->icon(); $this->assertEquals('Timber\\Image', get_class($icon)); $this->assertContains('cardinals.jpg', $icon->src()); }
function testCropHeight() { $arch = TestTimberImage::copyTestImage('arch.jpg'); $resized = TimberImageHelper::resize($arch, false, 250); $resized = str_replace('http://example.org', '', $resized); $resized = TimberUrlHelper::url_to_file_system($resized); $is_sized = TestTimberImage::checkSize($resized, 375, 250); $this->assertTrue($is_sized); }
function testFalseParent() { $pid = $this->factory->post->create(); $filename = TestTimberImage::copyTestImage('arch.jpg'); $attachment = array('post_title' => 'The Arch', 'post_content' => ''); $iid = wp_insert_attachment($attachment, $filename, $pid); update_post_meta($iid, 'architect', 'Eero Saarinen'); $image = new TimberImage($iid); $parent = $image->parent(); $this->assertEquals($pid, $parent->ID); $this->assertFalse($parent->parent()); }
function testJPEGtoJPG() { $filename = TestTimberImage::copyTestImage('jarednova.jpeg'); $str = Timber::compile_string('{{file|tojpg}}', array('file' => $filename)); $renamed = str_replace('.jpeg', '.jpg', $filename); $this->assertFileExists($renamed); $this->assertGreaterThan(1000, filesize($renamed)); $this->assertEquals('image/jpeg', mime_content_type($filename)); $this->assertEquals('image/jpeg', mime_content_type($renamed)); unlink($filename); unlink($renamed); }
function testImageResizeRetinaFilter() { $filename = TestTimberImage::copyTestImage('eastern.jpg'); $wp_filetype = wp_check_filetype(basename($filename), null); $post_id = $this->factory->post->create(); $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_excerpt' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $filename, $post_id); add_post_meta($post_id, '_thumbnail_id', $attach_id, true); $data = array(); $data['post'] = new TimberPost($post_id); $str = '{{post.thumbnail.src|resize(100, 50)|retina(3)}}'; $compiled = Timber::compile_string($str, $data); $img = new TimberImage($compiled); $this->assertContains('@3x', $compiled); $this->assertEquals(300, $img->width()); }
function testLetterboxSixCharHex() { $data = array(); $file_loc = TestTimberImage::copyTestImage('eastern.jpg'); $upload_dir = wp_upload_dir(); $new_file = TimberImageHelper::letterbox($upload_dir['url'] . '/eastern.jpg', 500, 500, '#FFFFFF', true); $location_of_image = TimberImageHelper::get_server_location($new_file); $this->assertTrue(TestTimberImage::checkSize($location_of_image, 500, 500)); //whats the bg/color of the image $image = imagecreatefromjpeg($location_of_image); $pixel_rgb = imagecolorat($image, 1, 1); $colors = imagecolorsforindex($image, $pixel_rgb); $this->assertEquals(255, $colors['red']); $this->assertEquals(255, $colors['blue']); $this->assertEquals(255, $colors['green']); }
function testGetAttachment() { $upload_dir = wp_upload_dir(); $post_id = $this->factory->post->create(); $filename = TestTimberImage::copyTestImage('flag.png'); $destination_url = str_replace(ABSPATH, 'http://' . $_SERVER['HTTP_HOST'] . '/', $filename); $wp_filetype = wp_check_filetype(basename($filename), null); $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $filename, $post_id); add_post_meta($post_id, '_thumbnail_id', $attach_id, true); $data = array(); $data['post'] = new TimberPost($post_id); $data['size'] = array('width' => 100, 'height' => 50); $data['crop'] = 'default'; Timber::compile('assets/thumb-test.twig', $data); $exists = file_exists($filename); $this->assertTrue($exists); $resized_path = $upload_dir['path'] . '/flag-' . $data['size']['width'] . 'x' . $data['size']['height'] . '-c-' . $data['crop'] . '.png'; $exists = file_exists($resized_path); $this->assertTrue($exists); $attachments = Timber::get_posts('post_type=attachment&post_status=inherit'); $this->assertGreaterThan(0, count($attachments)); }
function testDownloadURL() { if (!TestTimberImage::is_connected()) { $this->markTestSkipped('Cannot test external images when not connected to internet'); return; } $url = 'http://i1.nyt.com/images/misc/nytlogo379x64.gif'; $result = TimberURLHelper::download_url($url); $this->assertContains('/nytlogo379x64', $result); $this->assertStringEndsWith('.tmp', $result); }
function testWPMLurlLocal() { // this test replicates the url issue caused by the WPML language identifier in the url // However, WPML can't be installed with composer so this test mocks the WPML plugin // WPML uses a filter to alter the home_url $home_url_filter = function ($url) { return $url . '/en'; }; add_filter('home_url', $home_url_filter, -10, 4); // test with a local and external file $img = 'arch.jpg'; $img = TestTimberImage::copyTestImage($img); $resized = TimberImageHelper::resize($img, 50, 50); // make sure the base url has not been duplicated (https://github.com/timber/timber/issues/405) $this->assertLessThanOrEqual(1, substr_count($resized, 'example.org')); // make sure the image has been resized $resized = TimberUrlHelper::url_to_file_system($resized); $this->assertTrue(TestTimberImage::checkSize($resized, 50, 50), 'image should be resized'); }
function testIsNotGif() { $arch = TestTimberImage::copyTestImage('arch.jpg'); $this->assertFalse(TimberImageHelper::is_animated_gif($arch)); }