Commit 26d6c534 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Merge branch '28-add-writable-cache' into 'main'

Add writable cache directory in entrypoint

Closes #28

See merge request !31
parents 548c141e 9120581f
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -120,4 +120,9 @@ server {
		include safe.types;
		default_type application/octet-stream;
	}

	# Disable serving directly from any page cache in /wp-content/cache
	location /wp-content/cache/ {
		return 404;
	}
}
+12 −16
Original line number Diff line number Diff line
@@ -167,24 +167,20 @@ setup_components() {
	return 0
}

get_media_dir()
get_writable_dirs()
{
	[[ -v MEDIA ]] && return
	MEDIA=$(
	[[ -v MEDIA && -v CACHE ]] && return
	local content=$(
		wp config get WP_CONTENT_DIR --type=constant || echo wp-content
	)
	local media=$(
		wp config get UPLOADS --type=constant ||
		wp option get upload_path
	)
	[[ -n "${MEDIA}" ]] && return
	local _wp_content=$(wp config get WP_CONTENT_DIR --type=constant)
	MEDIA=${_wp_content:-wp-content}/uploads
}

setup_media()
{
	# UID values change on every run, ensure the owner and group are set
	# correctly on the media directory/volume.
	get_media_dir
	chown -R ${WORKER_USER}:${WORKER_USER} "${MEDIA}"
	MEDIA=${media:-${content}/uploads}
	CACHE=${content}/cache
	mkdir -p "${MEDIA}" "${CACHE}"
	chown -R ${WORKER_USER}:${WORKER_USER} "${MEDIA}" "${CACHE}"
}

setup_sandbox()
@@ -233,7 +229,7 @@ setup_debug()

collect_static()
{
	get_media_dir
	get_writable_dirs
	local IFS=,
	declare -a flags=(flist stats remove del)
	test -t 1 && flags+=(progress2)
@@ -244,6 +240,7 @@ collect_static()
		--exclude-from=- \
		--exclude='*.php' \
		--exclude="${MEDIA}" \
		--exclude="${CACHE}" \
		--exclude=/static/ \
		--exclude=/vendor/ \
		--force \
@@ -349,7 +346,6 @@ case "$1" in
		create_config
		setup_components
		setup_debug
		setup_media
		collect_static
		generate_static
		setup_sandbox
+33 −0
Original line number Diff line number Diff line
<?php
/*
Plugin Name: Test Plugin
Description: Assists in the testing of some scenarios
*/

add_action( 'rest_api_init', function() {
	register_rest_route( 'test/v1', '/write-cache', array(
		'methods' => WP_REST_Server::READABLE,
		'callback' => function($request) {
			if ( !isset($request['file']) ) {
				return new WP_Error('Missing param file');
			}
			$file = WP_CONTENT_DIR . "/cache/{$request['file']}";
			file_put_contents($file, "Generated file");
			return rest_ensure_response( "Cache file created" );
		},
		'args' => array(
			'file' => array(
				'description' => 'File to touch on the server host',
				'type' => 'string',
			)
		)
	));
});

/* register_activation_hook( __FILE__, function() { */
/* 	if ( !defined('WP_CLI') && $_SERVER['REQUEST_URI'] == '/test-cache' ) { */
/* 		$file = WP_CONTENT_DIR . "/cache/${_GET['file']}"; */
/* 		file_put_contents($file, "Generated file"); */
/* 		wp_die("Cache file created", 200); */
/* 	} */
/* }); */
+17 −0
Original line number Diff line number Diff line
@wip
Feature: Page cache directory
	Ensure that the page cache directory at WP_CONTENT_DIR/cache is writable for
	plugins, but is not served by the server.

	Scenario: After the entrypoint has completed, there is a writable cache dir
		Given the site is not running
		And test-cache.php is mounted in /app/wp-content/mu-plugins/
		When the site is started
		And /wp-json/test/v1/write-cache?file=foo.txt is requested
		Then OK is returned
		And /app/wp-content/cache/foo.txt exists in the backend

	Scenario: The contents of the cache dir are not served by the proxy
		Given /app/wp-content/cache/foo exists in the backend
		When /wp-content/cache/foo is requested
		Then Not Found is returned
+11 −0
Original line number Diff line number Diff line
@@ -231,6 +231,17 @@ def is_plugin_installed(
		assert site.backend.cli(addon.value, "is-active", name, query=True) == status.value


@then("{path:Path} exists in the {container_name}")
def check_file_exists(context: Context, path: Path, container_name: str) -> None:
	"""
	Check a file exists in the named container
	"""
	site = use_fixture(site_fixture, context)
	container = getattr(site, container_name)
	assert container.run(["sh", "-c", f"test -e {path}"]).returncode == 0, \
		f"{path} not found in the {container_name}"


@then("the email address of {user} is \"{value}\"")
@then("the email address of {user} is '{value}'")
def is_user_email(context: Context, user: str, value: str) -> None: