Skip to content

Commit 930a45d

Browse files
author
Jacob Middag
committed
Fix integration tests
1 parent 4c33314 commit 930a45d

11 files changed

Lines changed: 124 additions & 54 deletions

File tree

src/class-tiny-compress.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function get_status(&$details) {
4747
list($details, $headers) = $this->shrink(null);
4848

4949
$this->call_after_compress_callback($details, $headers);
50-
if ($details["error"] == 'InputMissing' || $details["error"] == 'TooManyRequests') {
50+
if (!isset($details['error']) || $details["error"] == 'InputMissing' || $details["error"] == 'TooManyRequests') {
5151
return true;
5252
} else {
5353
return false;

src/config/tiny-config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

33
class Tiny_Config {
4-
const URL = 'https://api.tinypng.com/shrink';
4+
const URL = 'http://webservice/shrink';
55
}

src/scripts/admin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
}
5757
if (error) {
5858
row.find('.bar').addClass('failed')
59-
row.find('.percent').html(tinyCompress.L10nError)
59+
row.find('.percent').html(tinyCompress.L10nInternalError)
6060
row.find('.percent').attr("title", error.toString())
6161
} else if (data.error) {
6262
row.find('.bar').addClass('failed')
63-
row.find('.percent').html(tinyCompress.L10nInternalError)
63+
row.find('.percent').html(tinyCompress.L10nError)
6464
row.find('.percent').attr("title", data.error)
6565
} else if (data.failed > 0) {
6666
row.find('.bar').addClass('failed')

test/helpers/setup.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,19 @@ function activate_plugin($driver) {
146146
}
147147

148148
function close_webdriver() {
149-
RemoteWebDriver::createBySessionId($GLOBALS['global_session_id'], $GLOBALS['global_webdriver_host'])->close();
149+
if (isset($GLOBALS['global_session_id']) && isset($GLOBALS['global_webdriver_host'])) {
150+
RemoteWebDriver::createBySessionId($GLOBALS['global_session_id'], $GLOBALS['global_webdriver_host'])->close();
151+
}
152+
}
153+
154+
function reset_webservice() {
155+
$request = curl_init();
156+
curl_setopt_array($request, array(
157+
CURLOPT_URL => 'http://' . getenv('HOST_IP') .':8080/reset',
158+
));
159+
160+
$response = curl_exec($request);
161+
curl_close($request);
150162
}
151163

152164
$global_webdriver_host = 'http://127.0.0.1:4444/wd/hub';

test/integration/BulkCompressIntegrationTest.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,46 @@ public function setUp() {
1111
public function tearDown() {
1212
clear_settings();
1313
clear_uploads();
14+
reset_webservice();
1415
}
1516

16-
public function testBulkCompressActionShouldBePresent()
17-
{
17+
public function testBulkCompressActionShouldBePresentInMedia() {
1818
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
1919
self::$driver->get(wordpress('/wp-admin/upload.php?mode=list'));
2020
$this->assertEquals('Compress all', self::$driver->findElement(
2121
WebDriverBy::cssSelector('select[name="action"] option[value="tiny_bulk_compress"]')
2222
)->getText());
2323
}
2424

25-
public function testBulkCompressShouldCompressUncompressedSizes() {
26-
$this->enable_compression_sizes(array('thumbnail'));
27-
25+
private function prepare($normal=1, $large=0) {
2826
$this->set_api_key('PNG123');
29-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-large.png');
27+
$this->enable_compression_sizes(array());
28+
29+
for ($i = 0; $i < $normal; $i++) {
30+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
31+
}
32+
for ($i = 0; $i < $large; $i++) {
33+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-large.png');
34+
}
35+
36+
$this->enable_compression_sizes(array('thumbnail', 'medium', 'large'));
37+
}
3038

31-
$this->enable_compression_sizes(array('thumbnail', 'medium'));
39+
public function testBulkCompressShouldInMediaShouldRedirect() {
40+
$this->prepare();
3241

3342
self::$driver->get(wordpress('/wp-admin/upload.php?mode=list'));
3443
$checkboxes = self::$driver->findElements(WebDriverBy::cssSelector('th input[type="checkbox"]'));
3544
$checkboxes[0]->click();
36-
self::$driver->findElement(WebDriverBy::cssSelector('select[name="action"] option[value="' . 'tiny_bulk_compress' . '"]'))->click();
45+
46+
self::$driver->findElement(WebDriverBy::cssSelector('select[name="action"] option[value="tiny_bulk_compress"]'))->click();
3747
self::$driver->findElement(WebDriverBy::cssSelector('div.actions input[value="Apply"]'))->click();
3848

39-
$this->assertContains('Compressed 2 out of 2 sizes', self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
49+
self::$driver->wait(2)->until(WebDriverExpectedCondition::textToBePresentInElement(
50+
WebDriverBy::cssSelector('.updated'), 'All images are processed'));
51+
52+
$this->assertContains("tools.php?page=tiny-bulk-compress&ids=", self::$driver->getCurrentUrl());
4053
}
54+
55+
// TODO: More tests
4156
}

test/integration/CompressIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testLimitReached() {
5353
$this->set_api_key('LIMIT123');
5454
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
5555
$this->assertContains('You have reached your limit',
56-
current(self::$driver->findElements(WebDriverBy::cssSelector('div.updated p')))->getText());
56+
self::$driver->findElement(WebDriverBy::cssSelector('div.error p'))->getText());
5757
}
5858

5959
public function testLimitReachedDismisses() {

test/integration/IntegrationTestCase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ protected function upload_image($path) {
3333
}
3434

3535
protected function set_api_key($api_key) {
36-
self::$driver->get(wordpress('/wp-admin/options-media.php'));
36+
$url = wordpress('/wp-admin/options-media.php');
37+
if (self::$driver->getCurrentUrl() != $url) {
38+
self::$driver->get($url);
39+
}
3740
self::$driver->findElement(WebDriverBy::name('tinypng_api_key'))->clear()->sendKeys($api_key);
3841
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
3942
return self::$driver->findElement(WebDriverBy::name('tinypng_api_key'));
4043
}
4144

4245
protected function enable_compression_sizes($sizes) {
43-
self::$driver->get(wordpress('/wp-admin/options-media.php'));
46+
$url = wordpress('/wp-admin/options-media.php');
47+
if (self::$driver->getCurrentUrl() != $url) {
48+
self::$driver->get($url);
49+
}
4450
$elements = self::$driver->findElements(WebDriverBy::xpath('//input[starts-with(@id, "tinypng_sizes_")]'));
4551
foreach($elements as $element) {
4652
$size = str_replace('tinypng_sizes_', '', $element->getAttribute('id'));

test/integration/SettingsIntegrationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ public function testShouldPersistNoSizes() {
8181
}
8282

8383
public function testStatusPresenceOK() {
84-
$this->set_api_key('STATUS123');
84+
reset_webservice();
85+
$this->set_api_key('PNG123');
8586
$elements = self::$driver->findElement(WebDriverBy::id('tiny-compress-status'))->findElements(WebDriverBy::tagName('p'));
8687
$statuses = array_map('innerText', $elements);
8788
$this->assertContains('API connection successful', $statuses);
88-
$this->assertContains('You have made 6 compressions this month.', $statuses);
89+
$this->assertContains('You have made 0 compressions this month.', $statuses);
8990
}
9091

9192
public function testStatusPresenseFail() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
RewriteEngine On
22
RewriteRule ^shrink$ shrink.php [last]
33
RewriteRule ^output/.+$ output.php [last]
4+
RewriteRule ^reset$ reset.php [last]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
define('SESSION_FILE', '/tmp/session.dat');
4+
5+
if (file_exists(SESSION_FILE)) {
6+
unlink(SESSION_FILE);
7+
}

0 commit comments

Comments
 (0)