Skip to content

Commit d2da131

Browse files
Add duplicate image tests.
1 parent 5928823 commit d2da131

5 files changed

Lines changed: 128 additions & 22 deletions

File tree

test/fixtures/json/_wp_attachment_metadata_duplicates.json

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
{
2-
"width": 1080,
3-
"height": 720,
4-
"file": "2015/09/panda.png",
2+
"width": 1256,
3+
"height": 1256,
4+
"file": "2015/09/tinypng_gravatar.png",
55
"sizes": {
6-
"custom-size": {
7-
"file": "panda-150x150.png",
8-
"width": 150,
9-
"height": 150,
6+
"small": {
7+
"file": "tinypng_gravatar-200x200.png",
8+
"width": 200,
9+
"height": 200,
1010
"mime-type": "image/png"
1111
},
12-
"custom-size-2": {
13-
"file": "panda-150x150.png",
14-
"width": 150,
15-
"height": 150,
12+
"medium": {
13+
"file": "tinypng_gravatar-300x300.png",
14+
"width": 300,
15+
"height": 300,
16+
"mime-type": "image/png"
17+
},
18+
"medium-2": {
19+
"file": "tinypng_gravatar-300x300.png",
20+
"width": 300,
21+
"height": 300,
22+
"mime-type": "image/png"
23+
},
24+
"large": {
25+
"file": "tinypng_gravatar-600x600.png",
26+
"width": 600,
27+
"height": 600,
28+
"mime-type": "image/png"
29+
},
30+
"custom-thumbnail": {
31+
"file": "tinypng_gravatar-175x175.png",
32+
"width": 175,
33+
"height": 175,
34+
"mime-type": "image/png"
35+
},
36+
"custom-thumbnail-2": {
37+
"file": "tinypng_gravatar-175x175.png",
38+
"width": 175,
39+
"height": 175,
40+
"mime-type": "image/png"
41+
},
42+
"custom-thumbnail-3": {
43+
"file": "tinypng_gravatar-175x175.png",
44+
"width": 175,
45+
"height": 175,
1646
"mime-type": "image/png"
1747
}
1848
},

test/helpers/wordpress.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct($vfs) {
6161
$this->addMethod( 'get_post_meta' );
6262
$this->addMethod( 'update_post_meta' );
6363
$this->addMethod( 'get_intermediate_image_sizes' );
64+
$this->addMethod( 'add_image_size' );
6465
$this->addMethod( 'translate' );
6566
$this->addMethod( 'load_plugin_textdomain' );
6667
$this->addMethod( 'get_post_mime_type' );
@@ -100,9 +101,11 @@ public function call($method, $args) {
100101
if ( 'translate' === $method ) {
101102
return $args[0];
102103
} elseif ( 'get_option' === $method ) {
103-
return call_user_func_array( array( $this->options, 'get'), $args );
104+
return call_user_func_array( array( $this->options, 'get' ), $args );
104105
} elseif ( 'get_post_meta' === $method ) {
105-
return call_user_func_array( array( $this, 'getMetadata'), $args );
106+
return call_user_func_array( array( $this, 'getMetadata' ), $args );
107+
} elseif ( 'add_image_size' === $method ) {
108+
return call_user_func_array( array( $this, 'addImageSize' ), $args );
106109
} elseif ( 'update_post_meta' === $method ) {
107110
return call_user_func_array( array( $this, 'updateMetadata'), $args );
108111
} elseif ( 'get_intermediate_image_sizes' === $method ) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
require_once dirname( __FILE__ ) . '/TinyTestCase.php';
4+
5+
class Tiny_Image_Duplicate_Test extends Tiny_TestCase {
6+
public function set_up() {
7+
parent::set_up();
8+
9+
$this->wp->addImageSize( 'medium-2', array( 'width' => 300, 'height' => 300 ) );
10+
$this->wp->addImageSize( 'custom-thumbnail', array( 'width' => 175, 'height' => 175 ) );
11+
$this->wp->addImageSize( 'custom-thumbnail-2', array( 'width' => 175, 'height' => 175 ) );
12+
13+
$this->wp->addOption( 'tinypng_sizes[medium-2]', 'on' );
14+
$this->wp->addOption( 'tinypng_sizes[custom-thumbnail]', 'off' );
15+
$this->wp->addOption( 'tinypng_sizes[custom-thumbnail-2]', 'on' );
16+
$this->wp->addOption( 'tinypng_sizes[custom-thumbnail-3]', 'on' );
17+
18+
$this->wp->createImagesFromJSON( $this->json( 'image_filesystem_data' ) );
19+
$this->wp->setTinyMetadata( 1, $this->json( 'image_database_metadata' ) );
20+
$this->subject = new Tiny_Image( 1, $this->json( '_wp_attachment_metadata_duplicates' ) );
21+
}
22+
23+
public function test_get_images_should_return_all_images() {
24+
$this->assertEquals( array(
25+
Tiny_Image::ORIGINAL,
26+
'medium',
27+
'thumbnail',
28+
'twentyfourteen-full-width',
29+
'custom-thumbnail',
30+
'custom-thumbnail-2',
31+
'custom-thumbnail-3',
32+
'failed',
33+
'large',
34+
'medium-2',
35+
'small',
36+
), array_keys( $this->subject->get_image_sizes() ) );
37+
}
38+
39+
public function test_filter_images_should_filter_correctly() {
40+
$this->assertEquals( array(
41+
Tiny_Image::ORIGINAL,
42+
'medium',
43+
), array_keys( $this->subject->filter_image_sizes( 'compressed' ) ) );
44+
}
45+
46+
public function test_filter_images_should_filter_duplicates_correctly() {
47+
$this->assertEquals( array(
48+
'medium-2',
49+
'custom-thumbnail',
50+
'custom-thumbnail-3',
51+
), array_keys( $this->subject->filter_image_sizes( 'is_duplicate' ) ) );
52+
}
53+
54+
public function test_duplicate_images_should_be_linked_to_primary_size() {
55+
$this->assertEquals(
56+
'medium',
57+
$this->subject->get_image_size( 'medium-2' )->duplicate_of_size()
58+
);
59+
60+
$this->assertEquals(
61+
'custom-thumbnail-2',
62+
$this->subject->get_image_size( 'custom-thumbnail' )->duplicate_of_size()
63+
);
64+
65+
$this->assertEquals(
66+
'custom-thumbnail-2',
67+
$this->subject->get_image_size( 'custom-thumbnail-3' )->duplicate_of_size()
68+
);
69+
}
70+
}

test/unit/TinyPluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function set_up() {
2020
$this->wp->addOption( 'tinypng_sizes[large]', 'on' );
2121
$this->wp->addOption( 'tinypng_sizes[post-thumbnail]', 'on' );
2222

23-
$this->wp->addImageSize( 'post-thumbnail', array( 'width' => 825, 'height' => 510) );
23+
$this->wp->addImageSize( 'post-thumbnail', array( 'width' => 825, 'height' => 510 ) );
2424
$this->wp->createImages();
2525
}
2626

test/unit/TinySettingsTest.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function test_should_retrieve_sizes_with_settings() {
3737
$this->wp->addOption( 'tinypng_sizes[0]', 'on' );
3838
$this->wp->addOption( 'tinypng_sizes[medium]', 'on' );
3939
$this->wp->addOption( 'tinypng_sizes[post-thumbnail]', 'on' );
40-
$this->wp->addImageSize( 'post-thumbnail', array( 'width' => 825, 'height' => 510) );
40+
$this->wp->addImageSize( 'post-thumbnail', array( 'width' => 825, 'height' => 510 ) );
4141

4242
global $_wp_additional_image_sizes;
4343
$_wp_additional_image_sizes = array( 'post-thumbnail' => array( 'width' => 825, 'height' => 510 ) );
@@ -75,34 +75,37 @@ public function test_should_set_all_sizes_on_without_configuration() {
7575
}
7676

7777
public function test_should_show_additional_size() {
78-
$this->wp->addImageSize( 'additional_size_1', array( 'width' => 666, 'height' => 333) );
78+
$this->wp->addImageSize( 'additional_size_1', array( 'width' => 666, 'height' => 333 ) );
7979
$this->subject->get_sizes();
8080
$sizes = $this->subject->get_sizes();
8181
$this->assertEquals(
8282
array( 'width' => 666, 'height' => 333, 'tinify' => true ),
83-
$sizes['additional_size_1']);
83+
$sizes['additional_size_1']
84+
);
8485
}
8586

8687
public function test_should_show_additional_size_without_height() {
87-
$this->wp->addImageSize( 'additional_size_no_height', array( 'width' => 777) );
88+
$this->wp->addImageSize( 'additional_size_no_height', array( 'width' => 777 ) );
8889
$this->subject->get_sizes();
8990
$sizes = $this->subject->get_sizes();
9091
$this->assertEquals(
9192
array( 'width' => 777, 'height' => 0, 'tinify' => true ),
92-
$sizes['additional_size_no_height']);
93+
$sizes['additional_size_no_height']
94+
);
9395
}
9496

9597
public function test_should_show_additional_size_without_width() {
96-
$this->wp->addImageSize( 'additional_size_no_width', array( 'height' => 888) );
98+
$this->wp->addImageSize( 'additional_size_no_width', array( 'height' => 888 ) );
9799
$this->subject->get_sizes();
98100
$sizes = $this->subject->get_sizes();
99101
$this->assertEquals(
100102
array( 'width' => 0, 'height' => 888, 'tinify' => true ),
101-
$sizes['additional_size_no_width']);
103+
$sizes['additional_size_no_width']
104+
);
102105
}
103106

104107
public function test_should_return_resize_enabled() {
105-
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on') );
108+
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on' ) );
106109
$this->assertEquals( true, $this->subject->get_resize_enabled() );
107110
}
108111

0 commit comments

Comments
 (0)