|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace lloc\Msls\ContentImport; |
|
4
|
|
|
|
|
5
|
|
|
use lloc\Msls\ContentImport\AttachmentPathFinder as Finder; |
|
6
|
|
|
|
|
7
|
|
|
class AttachmentPathFinderTest extends \Msls_UnitTestCase { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @test |
|
11
|
|
|
* it should be instantiatable |
|
12
|
|
|
*/ |
|
13
|
|
|
public function it_should_be_instantiatable() { |
|
14
|
|
|
$sut = $this->make_instance(); |
|
15
|
|
|
|
|
16
|
|
|
$this->assertInstanceOf( Finder::class, $sut ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return Finder |
|
21
|
|
|
*/ |
|
22
|
|
|
private function make_instance() { |
|
23
|
|
|
return new Finder(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Test filter_attachment_url with empty image date |
|
28
|
|
|
*/ |
|
29
|
|
|
public function test_filter_attachment_url_with_empty_image_date() { |
|
30
|
|
|
$sut = $this->make_instance(); |
|
31
|
|
|
$id = $this->factory->attachment->create(); |
|
32
|
|
|
|
|
33
|
|
|
$filtered = $sut->filter_attachment_url( '', $id ); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals( '', $filtered ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Test filter_attachment_url with not linked attachment |
|
40
|
|
|
*/ |
|
41
|
|
|
public function test_filter_attachment_url_with_not_linked_attachment() { |
|
42
|
|
|
$sut = $this->make_instance(); |
|
43
|
|
|
$id = $this->factory->attachment->create(); |
|
44
|
|
|
$input = 'http://example.com/images/image.jpg'; |
|
45
|
|
|
|
|
46
|
|
|
$filtered = $sut->filter_attachment_url( $input, $id ); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals( $input, $filtered ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Test filter_attachment_url with bad linked attachment data |
|
53
|
|
|
*/ |
|
54
|
|
|
public function test_filter_attachment_url_with_bad_linked_attachment_data() { |
|
55
|
|
|
$sut = $this->make_instance(); |
|
56
|
|
|
$id = $this->factory->attachment->create(); |
|
57
|
|
|
add_post_meta( $id, Finder::LINKED, 'foo-bar' ); |
|
58
|
|
|
$input = 'http://example.com/images/image.jpg'; |
|
59
|
|
|
|
|
60
|
|
|
$filtered = $sut->filter_attachment_url( $input, $id ); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals( $input, $filtered ); |
|
63
|
|
|
$this->assertEquals( '', get_post_meta( $id, Finder::LINKED, true ), 'The bad meta should be deleted.' ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Test filter_attachment_url with good format meta but bad data |
|
68
|
|
|
*/ |
|
69
|
|
|
public function test_filter_attachment_url_with_good_format_meta_but_bad_data() { |
|
70
|
|
|
$sut = $this->make_instance(); |
|
71
|
|
|
$id = $this->factory->attachment->create(); |
|
72
|
|
|
add_post_meta( $id, Finder::LINKED, [ 'glob' => 23, 'bar' => 89 ] ); |
|
73
|
|
|
$input = 'http://example.com/images/image.jpg'; |
|
74
|
|
|
|
|
75
|
|
|
$filtered = $sut->filter_attachment_url( $input, $id ); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals( $input, $filtered ); |
|
78
|
|
|
$this->assertEquals( '', get_post_meta( $id, Finder::LINKED, true ), 'The bad meta should be deleted.' ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Test filter_attachment_url with imported image |
|
83
|
|
|
*/ |
|
84
|
|
|
public function test_filter_attachment_url_with_imported_image() { |
|
85
|
|
|
$sut = $this->make_instance(); |
|
86
|
|
|
$id = $this->factory->attachment->create(); |
|
87
|
|
|
$source_blog = $this->factory->blog->create(); |
|
88
|
|
|
switch_to_blog( $source_blog ); |
|
|
|
|
|
|
89
|
|
|
$source_post = $this->factory->attachment->create_and_get(); |
|
90
|
|
|
restore_current_blog(); |
|
91
|
|
|
add_post_meta( $id, Finder::LINKED, [ 'blog' => $source_blog, 'post' => $source_post->ID ] ); |
|
92
|
|
|
$input = 'http://example.com/images/image.jpg'; |
|
93
|
|
|
|
|
94
|
|
|
$filtered = $sut->filter_attachment_url( $input, $id ); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals( $source_post->guid, $filtered ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Test filter_srcset with bad data |
|
102
|
|
|
*/ |
|
103
|
|
|
public function test_filter_srcset_with_bad_data() { |
|
104
|
|
|
$sut = $this->make_instance(); |
|
105
|
|
|
$id = $this->factory->attachment->create(); |
|
106
|
|
|
add_post_meta( $id, Finder::LINKED, [ 'foo' ] ); |
|
107
|
|
|
$sources = [ |
|
108
|
|
|
[ |
|
109
|
|
|
'url' => 'http://example.com/images/image.jpg' |
|
110
|
|
|
] |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
|
|
$filtered = $sut->filter_srcset( $sources, [], 'example.jpg', [], $id ); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals( $sources, $filtered ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Test filter_srcset |
|
120
|
|
|
*/ |
|
121
|
|
|
public function test_filter_srcset() { |
|
122
|
|
|
$sut = $this->make_instance(); |
|
123
|
|
|
$id = $this->factory->attachment->create(); |
|
124
|
|
|
$source_blog = $this->factory->blog->create(); |
|
125
|
|
|
switch_to_blog( $source_blog ); |
|
|
|
|
|
|
126
|
|
|
$source_post = $this->factory->attachment->create_upload_object( msls_test_data( 'images/image-one.jpg' ) ); |
|
127
|
|
|
restore_current_blog(); |
|
128
|
|
|
add_post_meta( $id, Finder::LINKED, [ 'blog' => $source_blog, 'post' => $source_post ] ); |
|
129
|
|
|
$sources = [ |
|
130
|
|
|
[ |
|
131
|
|
|
'url' => 'http://blog1.example.com/image-100x100.jpg', |
|
132
|
|
|
], |
|
133
|
|
|
[ |
|
134
|
|
|
'url' => 'http://blog1.example.com/image-150x300.jpg', |
|
135
|
|
|
] |
|
136
|
|
|
]; |
|
137
|
|
|
|
|
138
|
|
|
$filtered = $sut->filter_srcset( $sources, [], 'http://example.com/images/source-image.jpg', [], $id ); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertEquals( [ |
|
141
|
|
|
[ |
|
142
|
|
|
'url' => 'http://example.com/images/source-image-100x100.jpg', |
|
143
|
|
|
], |
|
144
|
|
|
[ |
|
145
|
|
|
'url' => 'http://example.com/images/source-image-150x300.jpg', |
|
146
|
|
|
] |
|
147
|
|
|
], $filtered ); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|