Skip to content

Commit 85a28c1

Browse files
committed
Adds some tests
1 parent d84c0ec commit 85a28c1

4 files changed

Lines changed: 87 additions & 2 deletions

File tree

core/ExceptionHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public static function dieWithHtmlErrorPage($exception)
8888

8989
// Log the error with an appropriate loglevel.
9090
switch (true) {
91-
case ($exception instanceof HttpCodeException && $exception->getCode() > 400 && $exception->getCode() < 500):
92-
// Do not log exception that results in 4xx HTTP status codes
91+
case ($exception instanceof HttpCodeException && $exception->getCode() >= 400 && $exception->getCode() < 500):
92+
// Log exceptions, resulting in 4xx HTTP status code, only at debug level
9393
self::logException($exception, Log::DEBUG);
9494
break;
9595
default:

tests/PHPUnit/Integration/AssetManager/UIAssetFetcher/PluginUmdAssetFetcherTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Piwik\AssetManager\UIAssetFetcher\Chunk;
1414
use Piwik\AssetManager\UIAssetFetcher\PluginUmdAssetFetcher;
1515
use Piwik\Filesystem;
16+
use Piwik\Http\BadRequestException;
1617
use Piwik\Plugin\Manager;
1718
use Piwik\Tests\Framework\TestCase\UnitTestCase;
1819

@@ -172,6 +173,17 @@ public function testGetChunkFilesWhenLoadingUmdsIndividually()
172173
$this->assertEquals($expectedChunkFiles, $actualChunkFiles);
173174
}
174175

176+
public function testGetChunkFilesThrows404WhenChunkIsMissing()
177+
{
178+
$plugins = array_keys(self::TEST_PLUGIN_DEPENDENCIES);
179+
$instance = new PluginUmdAssetFetcher($plugins, null, 'does-not-exist', false);
180+
181+
$this->expectException(BadRequestException::class);
182+
$this->expectExceptionCode(404);
183+
184+
$instance->getCatalog();
185+
}
186+
175187
public function testGetChunkFilesWhenLoadingUmdsIndividuallyAndNotAllPluginsActivated()
176188
{
177189
$plugins = array_keys(self::TEST_PLUGIN_DEPENDENCIES);

tests/PHPUnit/Integration/Plugin/ManagerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Piwik\Common;
1313
use Piwik\Config;
1414
use Piwik\Container\StaticContainer;
15+
use Piwik\Exception\PluginDeactivatedException;
16+
use Piwik\Exception\PluginNotFoundException;
1517
use Piwik\Http\ControllerResolver;
1618
use Piwik\Plugin;
1719
use Piwik\Cache as PiwikCache;
@@ -160,6 +162,37 @@ public function testIsPluginInstalledPluginInstalledConfigButNotExists()
160162
$this->assertTrue($this->manager->isPluginInstalled('FooBarBaz', false));
161163
}
162164

165+
public function testCheckIsPluginActivatedThrows404WhenPluginMissing()
166+
{
167+
$this->expectException(PluginNotFoundException::class);
168+
$this->expectExceptionCode(404);
169+
170+
$this->manager->checkIsPluginActivated('NotARealPlugin');
171+
}
172+
173+
public function testCheckIsPluginActivatedThrows403WhenPluginDeactivated()
174+
{
175+
$pluginName = 'ExampleTheme';
176+
$wasActivated = $this->manager->isPluginActivated($pluginName);
177+
178+
if ($wasActivated) {
179+
$this->manager->deactivatePlugin($pluginName);
180+
}
181+
182+
try {
183+
$this->assertTrue($this->manager->isPluginInFilesystem($pluginName));
184+
185+
$this->expectException(PluginDeactivatedException::class);
186+
$this->expectExceptionCode(403);
187+
188+
$this->manager->checkIsPluginActivated($pluginName);
189+
} finally {
190+
if ($wasActivated) {
191+
$this->manager->activatePlugin($pluginName);
192+
}
193+
}
194+
}
195+
163196
/**
164197
* @dataProvider getPluginNameProvider
165198
*/

tests/PHPUnit/System/ResponseCodeTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,46 @@ public function testProcessedApiShouldHaveCorrectHttpStatus()
101101
$this->assertEquals(200, $info['http_code']);
102102
}
103103

104+
public function testApiMissingRequiredParameterShouldReturn400()
105+
{
106+
[$response, $info] = $this->curl(
107+
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=API&method=SitesManager.getSiteFromId',
108+
['token_auth' => Fixture::ADMIN_USER_TOKEN]
109+
);
110+
111+
$this->assertEquals(400, $info['http_code']);
112+
$this->assertStringContainsString('Please specify a value for \'idSite\'', $response);
113+
}
114+
115+
public function testValidModuleInvalidActionShouldReturn404()
116+
{
117+
[$response, $info] = $this->curl(
118+
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=CoreHome&action=doesNotExist'
119+
);
120+
121+
$this->assertEquals(404, $info['http_code']);
122+
$this->assertStringContainsString("Action &#039;doesNotExist&#039; not found in the module &#039;CoreHome&#039;", $response);
123+
}
124+
125+
public function testMissingAssetChunkShouldReturn404()
126+
{
127+
[, $info] = $this->curl(
128+
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=Proxy&action=getPluginUmdJs&plugin=NotAPlugin'
129+
);
130+
131+
$this->assertEquals(404, $info['http_code']);
132+
}
133+
134+
public function testMissingPluginShouldReturn404HttpStatus()
135+
{
136+
[$response, $info] = $this->curl(
137+
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=NotAPlugin&action=index'
138+
);
139+
140+
$this->assertEquals(404, $info['http_code']);
141+
$this->assertStringContainsString('The plugin NotAPlugin was not found.', $response);
142+
}
143+
104144
private function curl($url, $postParams = [])
105145
{
106146
if (!function_exists('curl_init')) {

0 commit comments

Comments
 (0)