forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimberAttachment_UnitTestCase.php
108 lines (96 loc) · 3.2 KB
/
TimberAttachment_UnitTestCase.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
class TimberAttachment_UnitTestCase extends Timber_UnitTestCase
{
public $_files;
protected function addFile($file)
{
$this->_files[] = $file;
}
public function set_up()
{
parent::set_up();
$this->_files = [];
}
public function tear_down()
{
parent::tear_down();
if (isset($this->_files) && is_array($this->_files)) {
foreach ($this->_files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
$this->_files = [];
}
}
/* ----------------
* Helper functions
---------------- */
public static function replace_attachment($old_id, $new_id)
{
$uploadDir = wp_get_upload_dir();
$newFile = $uploadDir['basedir'] . '/' . get_post_meta($new_id, '_wp_attached_file', true);
$oldFile = $uploadDir['basedir'] . '/' . get_post_meta($old_id, '_wp_attached_file', true);
if (!file_exists(dirname($oldFile))) {
mkdir(dirname($oldFile), 0777, true);
}
copy($newFile, $oldFile);
$meta = wp_generate_attachment_metadata($old_id, $oldFile);
wp_update_attachment_metadata($old_id, $meta);
wp_delete_post($new_id, true);
}
public static function copyTestAttachment($img = 'arch.jpg', $dest_name = null)
{
$upload_dir = wp_get_upload_dir();
if (is_null($dest_name)) {
$dest_name = $img;
}
$destination = $upload_dir['path'] . '/' . $dest_name;
copy(__DIR__ . '/assets/' . $img, $destination);
return $destination;
}
public static function getTestAttachmentURL($img = 'arch.jpg', $relative = false)
{
$upload_dir = wp_get_upload_dir();
$result = $upload_dir['url'] . '/' . $img;
if ($relative) {
$result = str_replace(home_url(), '', $result);
}
return $result;
}
public static function is_connected()
{
$connected = @fsockopen("www.google.com", 80, $errno, $errstr, 3);
if ($connected) {
$is_conn = true; //action when connected
fclose($connected);
} else {
$is_conn = false; //action in connection failure
}
return $is_conn;
}
public function add_lang_to_home($url, $path, $orig_scheme, $blog_id)
{
return "$url?lang=en";
}
public static function get_attachment($pid = 0, $file = 'arch.jpg', $create_metadata = true)
{
$filename = self::copyTestAttachment($file);
$filetype = wp_check_filetype(basename($filename), null);
$attachment = [
'post_title' => 'The Arch',
'post_content' => '',
'post_mime_type' => $filetype['type'],
];
$iid = wp_insert_attachment($attachment, $filename, $pid);
if (!is_wp_error($iid) && $create_metadata) {
wp_update_attachment_metadata($iid, wp_generate_attachment_metadata($iid, $filename));
}
return $iid;
}
public static function get_timber_attachment_object($file = 'cropper.png')
{
$iid = self::get_attachment(0, $file);
return Timber::get_post($iid);
}
}