Skip to content

Commit

Permalink
Merge pull request #21 from bnf/authorship-refactoring-and-tests
Browse files Browse the repository at this point in the history
Authorship refactoring and tests
  • Loading branch information
bnf authored Jul 3, 2023
2 parents db9d969 + 1caa9f5 commit 40017a2
Show file tree
Hide file tree
Showing 26 changed files with 482 additions and 298 deletions.
6 changes: 6 additions & 0 deletions .build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/composer.lock
/config/sites/
/var
/public
/vendor
/assert*.sh
26 changes: 26 additions & 0 deletions .build/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "bnf/nginx-cache-test-site",
"repositories": [
{
"type": "path",
"url": "../"
},
{
"type": "path",
"url": "./packages/*/"
}
],
"license": "GPL-2.0+",
"require": {
"bnf/cache-status": "@dev",
"bnf/nginx-cache": "@dev",
"typo3/cms-adminpanel": "^12.4.0 || dev-main",
"typo3/minimal": "^12.4.0 || dev-main"
},
"config": {
"allow-plugins": {
"typo3/class-alias-loader": true,
"typo3/cms-composer-installers": true
}
}
}
1 change: 1 addition & 0 deletions .build/config/system/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/settings.php
38 changes: 38 additions & 0 deletions .build/config/system/additional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

if (getenv('IS_DDEV_PROJECT') == 'true') {
$GLOBALS['TYPO3_CONF_VARS'] = array_replace_recursive(
$GLOBALS['TYPO3_CONF_VARS'],
[
'DB' => [
'Connections' => [
'Default' => [
'dbname' => 'db',
'driver' => 'mysqli',
'host' => 'db',
'password' => 'db',
'port' => '3306',
'user' => 'db',
],
],
],
// This GFX configuration allows processing by installed ImageMagick 6
'GFX' => [
'processor' => 'ImageMagick',
'processor_path' => '/usr/bin/',
'processor_path_lzw' => '/usr/bin/',
],
// This mail configuration sends all emails to mailhog
'MAIL' => [
'transport' => 'smtp',
'transport_smtp_encrypt' => false,
'transport_smtp_server' => 'localhost:1025',
],
'SYS' => [
'trustedHostsPattern' => '.*.*',
'devIPmask' => '*',
'displayErrors' => 1,
],
]
);
}
16 changes: 16 additions & 0 deletions .build/packages/cache_status/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = tab
tab_width = 8

[**.php]
indent_style = space
indent_size = 4
41 changes: 41 additions & 0 deletions .build/packages/cache_status/Classes/CacheHit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Bnf\CacheStatus;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

final class CacheHit implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

if ($response instanceof NullResponse) {
return $response;
}

$tsfe = $request->getAttribute('frontend.controller', $GLOBALS['TSFE'] ?? null);
if (!$tsfe instanceof TypoScriptFrontendController) {
return $response;
}

return $response->withHeader(
'X-TYPO3-Cache',
$this->hitCache($tsfe) ? 'HIT' : 'MISS'
);
}

private function hitCache(TypoScriptFrontendController $tsfe): bool
{
return call_user_func(\Closure::bind(function() use ($tsfe) {
return $tsfe->pageContentWasLoadedFromCache ?? $tsfe->cacheContentFlag ?? false;
}, null, $tsfe));
}
}
11 changes: 11 additions & 0 deletions .build/packages/cache_status/Configuration/RequestMiddlewares.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
return [
'frontend' => [
'bnf/cache-status/cache-hit' => [
'target' => \Bnf\CacheStatus\CacheHit::class,
'before' => [
'typo3/cms-frontend/tsfe',
],
],
],
];
31 changes: 31 additions & 0 deletions .build/packages/cache_status/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "bnf/cache-status",
"type": "typo3-cms-extension",
"description": "Adds a X-TYPO3-Cache header to indicate cache hits/misses",
"keywords": [
"TYPO3",
"extension"
],
"authors": [
{
"name": "Benjamin Franzke",
"email": "[email protected]",
"role": "Developer",
"homepage": "https://bnf.dev"
}
],
"license": "GPL-2.0+",
"require": {
"typo3/cms-core": "^12.4.0"
},
"autoload": {
"psr-4": {
"Bnf\\CacheStatus\\": "Classes"
}
},
"extra": {
"typo3/cms": {
"extension-key": "cache_status"
}
}
}
29 changes: 29 additions & 0 deletions .build/packages/cache_status/ext_emconf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

$EM_CONF[$_EXTKEY] = array(
'title' => 'cache_status',
'description' => '',
'category' => 'Adds a X-TYPO3-Cache header to indicate cache hits/misses',
'author' => '',
'author_email' => '[email protected]',
'state' => 'stable',
'internal' => '',
'uploadfolder' => '0',
'createDirs' => '',
'clearCacheOnLoad' => 0,
'version' => '1.3.4',
'constraints' => array(
'depends' => array(
'typo3' => '10.4.0-12.4.99',
),
'conflicts' => array(
),
'suggests' => array(
),
),
'autoload' => array(
'psr-4' => array(
'Bnf\\CacheStatus\\' => 'Classes',
),
),
);
29 changes: 10 additions & 19 deletions .build/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

DIR=$(realpath $(dirname "$0"))
ROOT=$(realpath "$DIR/..")
if [ -z ${IS_DDEV_PROJECT+x} ]; then
HOST=${HOST:-http://localhost}
else
HOST="${DDEV_PRIMARY_URL}"
fi
HOST="${DDEV_PRIMARY_URL}"

make -C ${ROOT} .build/assert-1.1.sh

Expand All @@ -16,18 +12,15 @@ source $DIR/assert.sh
cookiefile=/tmp/nginx_cache-backend.cookie

function clear_cache() {
cd $ROOT
$ROOT/.build/vendor/bin/typo3cms cache:flush
cd -
curl -X PURGE $HOST/*
$ROOT/.build/vendor/bin/typo3 cache:flush
curl -X PURGE "${HOST}/*"
}

function test_hit() {
url=$1
nginx=$2
typo3=$3
args=$4

content=$(curl $args -s -o /dev/null -v "${HOST}${url}" 2>&1 | grep -- "< X-\(Cache\|TYPO3-Cache\):" | sed "s/\r//g")

echo "Testing nginx state for $content"
Expand All @@ -40,20 +33,18 @@ function test_hit() {
function login() {
rm -f $cookiefile
data=$(curl -s -L -c $cookiefile "${HOST}/typo3/")
login_provider=$(echo $data | sed -n "s|.*\\(?loginProvider=[0-9]*\).*|\1|p")
login_provider=$(echo $data | sed -n "s|.*\(?loginProvider=[0-9]*\).*|\1|p")
request_token=$(echo $data | sed -n 's/.*<input type="hidden" name="__RequestToken" value="\([^"]*\)".*/\1/p')

code=$(curl -s -w "%{http_code}" -X POST -b $cookiefile -c $cookiefile -e ${HOST}/typo3/ --data "login_status=login&userident=password&username=admin&interface=backend" "${HOST}/typo3/${login_provider}")
code=$(curl -s -w '%{http_code}' -X POST -b $cookiefile -c $cookiefile -e ${HOST}/typo3/ --data "login_status=login&userident=Pa%24%24w0rd&username=admin&__RequestToken=${request_token}" "${HOST}/typo3/${login_provider}" )

if [ "$code" != 303 ]; then
echo $code
(>&2 echo "Login failed.")
exit 1
fi
}


test -f .build/public/typo3conf/PackageStates.php && $ROOT/.build/vendor/bin/typo3cms extension:deactivate rsaauth || true
test -f .build/public/typo3conf/PackageStates.php && $ROOT/.build/vendor/bin/typo3cms configuration:remove BE/loginSecurityLevel --force || true

rm -f $cookiefile

clear_cache
Expand All @@ -77,7 +68,7 @@ assert_raises "test_hit / MISS HIT -I"
assert_raises "test_hit / MISS HIT"
assert_raises "test_hit / HIT HIT -I"

echo "UPDATE sys_template set config = REPLACE(config, 'config.admPanel = 1\n', '')" | $ROOT/.build/vendor/bin/typo3cms database:import
echo "UPDATE sys_template set config = REPLACE(config, 'config.admPanel = 1\n', '')" | mysql db
clear_cache
login
assert_raises "test_hit / BYPASS MISS '-b $cookiefile -c $cookiefile'"
Expand All @@ -86,7 +77,7 @@ assert_raises "test_hit / BYPASS HIT '-b $cookiefile -c $cookiefile'"
#test_hit / MISS HIT
assert_raises "test_hit / HIT HIT"

echo "UPDATE sys_template set config = REPLACE(config, 'page = PAGE', 'config.admPanel = 1\npage = PAGE')" | $ROOT/.build/vendor/bin/typo3cms database:import
echo "UPDATE sys_template set config = REPLACE(config, 'page = PAGE', 'config.admPanel = 1\npage = PAGE')" | mysql db
clear_cache
login
assert_raises "test_hit / BYPASS MISS '-b $cookiefile -c $cookiefile'"
Expand All @@ -95,7 +86,7 @@ assert_raises "test_hit / MISS HIT"
assert_raises "test_hit / BYPASS HIT '-b $cookiefile -c $cookiefile'"
assert_raises "test_hit / HIT HIT"

#echo "UPDATE sys_template set config = REPLACE(config, 'page = PAGE', 'config.admPanel = 1\npage = PAGE')" | $ROOT/.build/vendor/bin/typo3cms database:import
#echo "UPDATE sys_template set config = REPLACE(config, 'page = PAGE', 'config.admPanel = 1\npage = PAGE')" | mysql db
clear_cache
login
assert_raises "test_hit / BYPASS MISS '-b $cookiefile -c $cookiefile'"
Expand Down
Loading

0 comments on commit 40017a2

Please sign in to comment.