Skip to content

Commit 7db683d

Browse files
author
Jacob Middag
committed
Add and improve integration testing
1 parent 96ff9d5 commit 7db683d

8 files changed

Lines changed: 109 additions & 25 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Got questions or feedback? Let us know! Contact us at support@tinypng.com.
2828

2929
### Running the integration tests
3030
1. Start Selenium server: `java -jar selenium-server-standalone-2.44.0.jar`.
31-
2. Run `bin/test-wordpress <version>`. E.g. `bin/test-wordpress 41`.
31+
2. Run `bin/integration-tests $version [$to_version]`. E.g. `bin/run-wordpress 41` or `bin/integration-tests 40 42`.
3232

3333
Note that when testing a different WordPress version, `bin/run-wordpress <version>` has to be run first.
3434

bin/format-language-files

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use strict;
44
use warnings;
5-
use File::Basename;
5+
use File::Basename qw(dirname fileparse);
66

77
my $dir = dirname($0);
88
open FIND, "find $dir/../src/languages -type f -name '*.po' |";

bin/integration-tests

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use File::Basename qw(dirname);
6+
use IO::Socket;
7+
use Time::HiRes qw(usleep);
8+
9+
my $dir = dirname($0);
10+
my $hostip = `boot2docker ip`;
11+
12+
sub check_port {
13+
my ($ip, $port, $times, $sleep) = @_;
14+
for (my $i = 0; $i < $times; $i++) {
15+
my $sock = IO::Socket::INET->new(
16+
PeerAddr => $ip,
17+
PeerPort => $port,
18+
Proto => 'tcp',
19+
Timeout => 2
20+
);
21+
return 1 if $sock;
22+
usleep($sleep * 1000);
23+
}
24+
return 0;
25+
}
26+
27+
my $from = $ARGV[0] || 42;
28+
my $to = $ARGV[1] || $from;
29+
30+
for (my $version = $from; $version <= $to; $version++) {
31+
printf("==================== Wordpress %d ====================\n", $version);
32+
print(" - Running\n");
33+
my $ret = system("$dir/run-wordpress $version > /dev/null 2>&1");
34+
if ($ret) {
35+
print(" Error: $ret\n");
36+
next;
37+
}
38+
print(" - Waiting\n");
39+
sleep(10);
40+
print(" - Checking\n");
41+
if (!check_port($hostip, 8000 + $version, 10, 500)) {
42+
print(" Error: No open port\n");
43+
next;
44+
}
45+
print(" - Testing\n");
46+
$ret = system("$dir/test-wordpress $version");
47+
if ($ret) {
48+
print(" Error: $ret\n");
49+
next;
50+
}
51+
}

test/helpers/setup.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ function set_siteurl($site_url) {
6666
$statement->execute();
6767
}
6868

69+
function clear_settings() {
70+
$db = new mysqli(getenv('HOST_IP'), 'root', getenv('MYSQL_ROOT_PASSWORD'), getenv('WORDPRESS_DATABASE'));
71+
$statement = $db->prepare("DELETE FROM wp_options WHERE option_name LIKE 'tinypng_%'");
72+
$statement->execute();
73+
$statement = $db->prepare("DELETE FROM wp_usermeta WHERE meta_key LIKE 'tinypng_%'");
74+
$statement->execute();
75+
}
76+
77+
function clear_uploads() {
78+
$db = new mysqli(getenv('HOST_IP'), 'root', getenv('MYSQL_ROOT_PASSWORD'), getenv('WORDPRESS_DATABASE'));
79+
$statement = $db->prepare("DELETE wp_postmeta FROM wp_postmeta JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE wp_posts.post_type = 'attachment'");
80+
$statement->execute();
81+
$statement = $db->prepare("DELETE FROM wp_posts WHERE wp_posts.post_type = 'attachment'");
82+
$statement->execute();
83+
shell_exec('docker exec -it wordpress' . getenv('WORDPRESS_VERSION') . ' rm -rf wp-content/uploads');
84+
}
85+
6986
function is_wordpress_setup() {
7087
$db = new mysqli(getenv('HOST_IP'), 'root', getenv('MYSQL_ROOT_PASSWORD'));
7188
if ($result = $db->query("SELECT * FROM information_schema.tables WHERE table_schema = '" . getenv('WORDPRESS_DATABASE') . "'")) {
@@ -100,20 +117,6 @@ function setup_wordpress_site($driver) {
100117
}
101118
}
102119

103-
function clear_uploads($driver) {
104-
media_bulk_action($driver, 'delete');
105-
}
106-
107-
function media_bulk_action($driver, $action) {
108-
$driver->get(wordpress('/wp-admin/upload.php?mode=list'));
109-
$checkboxes = $driver->findElements(WebDriverBy::cssSelector('th input[type="checkbox"]'));
110-
if (count($checkboxes) > 0) {
111-
$checkboxes[0]->click();
112-
$driver->findElement(WebDriverBy::cssSelector('select[name="action"] option[value="' . $action . '"]'))->click();
113-
$driver->findElement(WebDriverBy::cssSelector('div.actions input[value="Apply"]'))->click();
114-
}
115-
}
116-
117120
function login($driver) {
118121
$driver->get(wordpress('/wp-login.php'));
119122
$driver->findElement(WebDriverBy::tagName('body'))->click();
@@ -142,9 +145,15 @@ function activate_plugin($driver) {
142145
}
143146
}
144147

145-
register_shutdown_function('restore_wordpress');
148+
function close_webdriver() {
149+
RemoteWebDriver::createBySessionId($GLOBALS['global_session_id'], $GLOBALS['global_webdriver_host'])->close();
150+
}
146151

147-
$global_phantom_host = 'http://127.0.0.1:4444/wd/hub';
148-
$global_driver = RemoteWebDriver::create($global_phantom_host, DesiredCapabilities::firefox());
152+
$global_webdriver_host = 'http://127.0.0.1:4444/wd/hub';
153+
$global_driver = RemoteWebDriver::create($global_webdriver_host, DesiredCapabilities::firefox());
149154
$global_session_id = $global_driver->getSessionID();
155+
156+
register_shutdown_function('close_webdriver');
157+
register_shutdown_function('restore_wordpress');
158+
150159
configure_wordpress_for_testing($global_driver);

test/integration/BulkCompressIntegrationTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public function setUp() {
99
}
1010

1111
public function tearDown() {
12-
$this->enable_compression_sizes(array('0', 'thumbnail', 'medium', 'large', 'post-thumbnail'));
13-
clear_uploads(self::$driver);
12+
clear_settings();
13+
clear_uploads();
1414
}
1515

1616
public function testBulkCompressActionShouldBePresent()
@@ -29,7 +29,12 @@ public function testBulkCompressShouldCompressUncompressedSizes() {
2929
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-large.png');
3030

3131
$this->enable_compression_sizes(array('thumbnail', 'medium'));
32-
media_bulk_action(self::$driver, 'tiny_bulk_compress');
32+
33+
self::$driver->get(wordpress('/wp-admin/upload.php?mode=list'));
34+
$checkboxes = self::$driver->findElements(WebDriverBy::cssSelector('th input[type="checkbox"]'));
35+
$checkboxes[0]->click();
36+
self::$driver->findElement(WebDriverBy::cssSelector('select[name="action"] option[value="' . 'tiny_bulk_compress' . '"]'))->click();
37+
self::$driver->findElement(WebDriverBy::cssSelector('div.actions input[value="Apply"]'))->click();
3338

3439
$this->assertContains('Compressed 2 out of 2 sizes', self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
3540
}

test/integration/CompressIntegrationTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public function setUp() {
99
}
1010

1111
public function tearDown() {
12-
clear_uploads(self::$driver);
12+
clear_settings();
13+
clear_uploads();
1314
}
1415

1516
public function testInvalidCredentialsShouldStillUploadImage()
@@ -35,6 +36,19 @@ public function testShrink() {
3536
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
3637
}
3738

39+
public function testCompressButton() {
40+
$this->enable_compression_sizes(array('medium'));
41+
$this->set_api_key('PNG123');
42+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
43+
$this->enable_compression_sizes(array('medium', 'large'));
44+
self::$driver->get(wordpress('/wp-admin/upload.php?mode=list'));
45+
$this->assertContains('Compressed 1 out of 2 sizes',
46+
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
47+
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images button'))->click();
48+
self::$driver->wait(2)->until(WebDriverExpectedCondition::textToBePresentInElement(
49+
WebDriverBy::cssSelector('td.tiny-compress-images'), 'Compressed 2 out of 2 sizes'));
50+
}
51+
3852
public function testLimitReached() {
3953
$this->set_api_key('LIMIT123');
4054
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
@@ -46,6 +60,7 @@ public function testLimitReachedDismisses() {
4660
$this->set_api_key('LIMIT123');
4761
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
4862
self::$driver->findElement(WebDriverBy::cssSelector('a.tiny-dismiss'))->click();
49-
self::$driver->wait(2)->until(WebDriverExpectedCondition::invisibilityOfElementWithText(WebDriverBy::cssSelector('a.tiny-dismiss'), 'Dismiss'));
63+
self::$driver->wait(2)->until(WebDriverExpectedCondition::invisibilityOfElementWithText(
64+
WebDriverBy::cssSelector('a.tiny-dismiss'), 'Dismiss'));
5065
}
5166
}

test/integration/IntegrationTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ abstract class IntegrationTestCase extends PHPUnit_Framework_TestCase {
88
protected static $driver;
99

1010
public static function setUpBeforeClass() {
11-
self::$driver = RemoteWebDriver::createBySessionId($GLOBALS['global_session_id'], $GLOBALS['global_phantom_host']);
11+
self::$driver = RemoteWebDriver::createBySessionId($GLOBALS['global_session_id'], $GLOBALS['global_webdriver_host']);
1212
}
1313

1414
protected function upload_image($path) {

test/integration/SettingsIntegrationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public function setUp() {
99
self::$driver->get(wordpress('/wp-admin/options-media.php'));
1010
}
1111

12+
public function tearDown() {
13+
clear_settings();
14+
}
15+
1216
public function testTitlePresence()
1317
{
1418
$h3s = self::$driver->findElements(WebDriverBy::tagName('h3'));

0 commit comments

Comments
 (0)