forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-wp-vip.php
85 lines (81 loc) · 2.97 KB
/
test-timber-wp-vip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* @group attachments
*/
class TestTimberWPVIP extends TimberAttachment_UnitTestCase
{
public function testDisableCache()
{
$filter = function () {
return 'none';
};
add_filter('timber/cache/mode', $filter);
$loader = new Timber\Loader();
$cache = $loader->set_cache('test', 'foobar');
$cache = $loader->get_cache('test');
$this->assertFalse($cache);
remove_filter('timber/cache/mode', $filter);
}
public function testImageResize()
{
add_filter('timber/allow_fs_write', '__return_false');
$data = [];
$data['size'] = [
'width' => 600,
'height' => 400,
];
$upload_dir = wp_upload_dir();
self::copyTestAttachment();
$url = $upload_dir['url'] . '/arch.jpg';
$data['test_image'] = $url;
$data['crop'] = 'default';
Timber::compile('assets/image-test.twig', $data);
$resized_path = $upload_dir['path'] . '/arch-' . $data['size']['width'] . 'x' . $data['size']['height'] . '-c-' . $data['crop'] . '.jpg';
$this->assertFileDoesNotExist($resized_path);
remove_filter('timber/allow_fs_write', '__return_false');
}
public function testImageResizeInTwig()
{
add_filter('timber/allow_fs_write', '__return_false');
$pid = $this->factory->post->create([
'post_type' => 'post',
]);
$attach_id = self::get_attachment($pid, 'arch.jpg');
$template = '<img src="{{get_post(img).src|resize(200, 200)}}">';
$str = Timber::compile_string($template, [
'img' => $attach_id,
]);
$this->assertEquals('<img src="http://example.org/wp-content/uploads/' . date('Y/m') . '/arch.jpg">', $str);
remove_filter('timber/allow_fs_write', '__return_false');
}
public function testImageSrcThumbnail()
{
add_filter('timber/allow_fs_write', '__return_false');
require_once('wp-overrides.php');
$filename = __DIR__ . '/assets/arch.jpg';
$filesize = filesize($filename);
$data = [
'tmp_name' => $filename,
'name' => 'arch.jpg',
'type' => 'image/jpg',
'size' => $filesize,
'error' => 0,
];
$this->assertTrue(file_exists($filename));
$_FILES['tester'] = $data;
$file_id = WP_Overrides::media_handle_upload('tester', 0, [], [
'test_form' => false,
]);
if (!is_int($file_id)) {
error_log(print_r($file_id, true));
}
$image = Timber::get_post($file_id);
$str = '<img src="{{image.src(\'medium\')}}" />';
$result = Timber::compile_string($str, [
'image' => $image,
]);
$upload_dir = wp_upload_dir();
$this->assertEquals('<img src="' . $upload_dir['url'] . '/' . $image->sizes['medium']['file'] . '" />', trim($result));
remove_filter('timber/allow_fs_write', '__return_false');
}
}