1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-21 22:47:43 +00:00
Files
matomo/config/environment/ui-test.php
Michal Kleiner 6724be0ce2 Implement logo upload via temp folder and publish only when settings are saved (#23268)
* Implement logo upload via temp folder and publish only when settings are saved

* Update UI test screenshot

* Update UI test screenshot

* Add UI tests for custom logo upload

* Tweak UI tests config to cater for empty result var

* Move UI test into a separate spec file and fix settings save button selector

* Use sha1 to hash username to remove unsafe characters and obscure the username in temp folder

* Update UI test to move fs expectations to the same unit after screenshot checks

* Update UI test screenshots from UI

* Fix expected tmp path to account for sha1 of the login

* Remove temp files on page load to prevent accidental upload

* Fix bug where custom logos enabled without any files

* Update upload logic, only a save with present images is valid.

* Clean up existing published logos before saving new ones

* Allow uploads even when either logo or favicon present

* Fix bug, check was ignoring temp favicon existence

* Use separate handling for logo and favicon so that updating one doesn't break the other

* Update UI tests and fix settings saving after page reload

* Fix CS and update screenshots from CI

* Add strict types

Co-authored-by: Nathan Gavin <nathangavin987@gmail.com>

* Use early return and simplify conditions

* Ensure custom favicon display is controlled independently from the custom logo

---------

Co-authored-by: Nathan Gavin <nathangavin987@gmail.com>
Co-authored-by: caddoo <1169490+caddoo@users.noreply.github.com>
2025-05-20 16:39:18 +12:00

151 خطوط
5.8 KiB
PHP

<?php
use Piwik\Container\Container;
use Piwik\Container\StaticContainer;
use Piwik\DataTable;
use Piwik\Plugin\Visualization;
use Piwik\Plugins\Diagnostics\Diagnostic\FileIntegrityCheck;
use Piwik\Plugins\Diagnostics\Diagnostic\PhpVersionCheck;
use Piwik\Plugins\Diagnostics\Diagnostic\RequiredPrivateDirectories;
use Piwik\SiteContentDetector;
use Piwik\Tests\Framework\Mock\FakeSiteContentDetector;
return [
// UI tests will remove the port from all URLs to the test server. if a test
// requires the ports in UI tests (eg, Overlay), add the api/controller methods
// to one of these blacklists
'tests.ui.url_normalizer_blacklist.api' => [],
'tests.ui.url_normalizer_blacklist.controller' => [],
// disable check for plugin updates during UI tests, allow for override
'dev.forced_plugin_update_result' => Piwik\DI::decorate(function ($previous, Container $c) {
return $c->get('test.vars.forceEnablePluginUpdateChecks') ? null : [];
}),
'twig.cache' => function (\Piwik\Container\Container $container) {
$templatesPath = $container->get('path.tmp.templates');
return new class ($templatesPath) extends \Twig\Cache\FilesystemCache {
public function write(string $key, string $content): void
{
$retryCount = 3;
$attempts = 0;
while ($attempts < $retryCount) {
try {
parent::write($key, $content);
return;
} catch (\Exception $ex) {
if (!preg_match('/^Failed to write cache file/', $ex->getMessage())) {
throw $ex;
}
usleep(50);
++$attempts;
}
}
}
};
},
'Piwik\Config' => \Piwik\DI::decorate(function (\Piwik\Config $config, Container $c) {
$config->General['cors_domains'][] = '*';
$config->General['trusted_hosts'][] = '127.0.0.1';
$config->General['trusted_hosts'][] = $config->tests['http_host'];
$config->General['trusted_hosts'][] = $config->tests['http_host'] . ':' . $config->tests['port'];
// disable plugin promos for UI tests, only enable when explicitly requested
if ($c->get('test.vars.enableProfessionalSupportAdsForUITests')) {
$config->General['piwik_professional_support_ads_enabled'] = '1';
} else {
$config->General['piwik_professional_support_ads_enabled'] = '0';
}
return $config;
}),
// avoid any site content detection checks
SiteContentDetector::class => \Piwik\DI::decorate(function ($previous, Container $c) {
$detectedContentDetections = $c->get('test.vars.detectedContentDetections') ?: [];
$connectedConsentManagers = $c->get('test.vars.connectedConsentManagers') ?: [];
return new FakeSiteContentDetector($detectedContentDetections, $connectedConsentManagers);
}),
'observers.global' => \Piwik\DI::add([
// removes port from all URLs to the test Piwik server so UI tests will pass no matter
// what port is used
['Request.dispatch.end', Piwik\DI::value(function (&$result) {
$request = $_GET + $_POST;
$apiblacklist = StaticContainer::get('tests.ui.url_normalizer_blacklist.api');
if (!empty($request['method'])
&& in_array($request['method'], $apiblacklist)
) {
return;
}
$controllerActionblacklist = StaticContainer::get('tests.ui.url_normalizer_blacklist.controller');
if (!empty($request['module'])) {
$controllerAction = $request['module'] . '.' . (isset($request['action']) ? $request['action'] : 'index');
if (in_array($controllerAction, $controllerActionblacklist)) {
return;
}
}
$config = \Piwik\Config::getInstance();
$host = $config->tests['http_host'];
$port = $config->tests['port'];
if (!empty($port)) {
// remove the port from URLs if any so UI tests won't fail if the port isn't 80
$result = str_replace($host . ':' . $port, $host, $result ?? '');
}
// remove PIWIK_INCLUDE_PATH from result so tests don't change based on the machine used
$path = realpath(PIWIK_INCLUDE_PATH);
$pathInJson = str_replace('/', '\\/', $path);
$result = str_replace([$path, $pathInJson], '', $result ?? '');
})],
['Controller.RssWidget.rssPiwik.end', Piwik\DI::value(function (&$result, $parameters) {
$result = '';
})],
['Visualization.beforeRender', Piwik\DI::value(function (Visualization $visualization) {
$dataStates = StaticContainer::get('test.vars.forceDataStates');
if (!is_array($dataStates) || [] === $dataStates) {
return;
}
$dataTable = $visualization->getDataTable();
if (!($dataTable instanceof DataTable\Map)) {
return;
}
foreach ($dataTable->getDataTables() as $date => $subTable) {
if (!isset($dataStates[$date])) {
continue;
}
$subTable->setMetadata(
DataTable::ARCHIVE_STATE_METADATA_NAME,
$dataStates[$date]
);
}
})],
\Piwik\Tests\Framework\XssTesting::getJavaScriptAddEvent(),
]),
// disable some diagnostics for UI tests
'diagnostics.disabled' => \Piwik\DI::add([
\Piwik\DI::get(FileIntegrityCheck::class),
\Piwik\DI::get(RequiredPrivateDirectories::class),
\Piwik\DI::get(PhpVersionCheck::class),
]),
];