diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..c49eaff --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,51 @@ +name: Tests + +on: + push: + branches: [ main, '[0-9]+.[0-9]+' ] + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + functional-tests: + name: Functional tests (Neos ${{ matrix.neos-version }}, PHP ${{ matrix.php-version }}) + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + # the lowest and the newest supported Neos 9 line, each with its minimum PHP version + include: + - neos-version: '9.0' + php-version: '8.2' + - neos-version: '9.2' + php-version: '8.4' + + steps: + - uses: actions/checkout@v7 + + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: mbstring, intl, pdo_sqlite + tools: composer:v2 + coverage: none + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT" + + - uses: actions/cache@v6 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: composer-${{ runner.os }}-neos${{ matrix.neos-version }}-php${{ matrix.php-version }}-${{ hashFiles('composer.json', 'Tests/Functional/setup-test-distribution.sh') }} + restore-keys: composer-${{ runner.os }}-neos${{ matrix.neos-version }}-php${{ matrix.php-version }}- + + - name: Run functional tests + # builds a throwaway Neos distribution with this package symlinked into it, see the script + run: Tests/Functional/run-tests.sh "$RUNNER_TEMP/test-distribution" + env: + NEOS_VERSION: ${{ matrix.neos-version }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..403744e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# IDE settings +.idea/ diff --git a/Classes/Fusion/PaginationArrayImplementation.php b/Classes/Fusion/PaginationArrayImplementation.php index c7642f4..72420d7 100644 --- a/Classes/Fusion/PaginationArrayImplementation.php +++ b/Classes/Fusion/PaginationArrayImplementation.php @@ -19,7 +19,7 @@ public function evaluate(): array if ($totalCount > 0 !== true) { return []; } - $numberOfPages = ceil($totalCount / $itemsPerPage); + $numberOfPages = (int)ceil($totalCount / $itemsPerPage); if ($maximumNumberOfLinks > $numberOfPages) { $maximumNumberOfLinks = $numberOfPages; } @@ -35,12 +35,16 @@ public function evaluate(): array $displayRangeStart = (integer)max($displayRangeStart, 1); $displayRangeEnd = (integer)min($displayRangeEnd, $numberOfPages); $links = \range($displayRangeStart, $displayRangeEnd); - if ($displayRangeStart > 2) { - array_unshift($links, '...'); + if ($displayRangeStart > 1) { + if ($displayRangeStart > 2) { + array_unshift($links, '...'); + } array_unshift($links, 1); } - if ($displayRangeEnd + 1 < $numberOfPages) { - $links[] = '...'; + if ($displayRangeEnd < $numberOfPages) { + if ($displayRangeEnd + 1 < $numberOfPages) { + $links[] = '...'; + } $links[] = $numberOfPages; } diff --git a/README.md b/README.md index 59a06ee..a08fe76 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,15 @@ The idea is very simple: you often need to display list of things (e.g. news, ar 5. For each of your nodetypes create a new Fusion object of type NodeTypeName + '.Short', or manually define a rendering object. 6. Rely on public API keys when overriding settings. +# Versioning scheme + +| Package Version | Branch | Neos / Flow Version | PHP Version | Supported | Remarks | +|-----------------------------|--------|---------------------|-------------|-----------|-----------------------------------------------------------------| +| 4.x (current `main` branch) | `main` | 9.x | >= 8.2 | ☑️ | Neos 9 / new Content Repository. | +| 3.6.x | `3.6` | 3.3 – 8.x | | ☑️ | Bugfixes for Neos 8 (and older) installations. | +| 3.0 – 3.5 | | 3.x – 7.x | | ⛔️ | Superseded by 3.6.x. | +| 2.x and older | | 3.x and older | | ⛔️ | | + # Fusion objects Keys documented here are considered public API and would be treated with semantic versioning in mind. Extend all other properties at your own risk. @@ -154,3 +163,21 @@ Sort nodes by their position in the node tree. Please use with care, as this can Example: ${q(node).children("main").sortRecursiveByIndex('DESC').get()} + +# Running the tests + +The functional tests boot a Flow application, so they run inside a throwaway Neos distribution with this package +symlinked into it. The script creates the distribution on the first run (in your temp directory) and reuses it +afterwards: + + Tests/Functional/run-tests.sh + + # custom distribution directory and additional PHPUnit arguments + Tests/Functional/run-tests.sh /tmp/listable-tests -- --filter PaginationArrayTest + + # another Neos 9 minor version (default: 9.2) + NEOS_VERSION=9.0 Tests/Functional/run-tests.sh + +Delete the distribution directory to start from scratch. The same script runs in the GitHub CI +(`.github/workflows/tests.yml`), against the lowest and the newest supported Neos version. The `3.6` branch has +the same setup for Neos 8.3. diff --git a/Tests/Functional/Fusion/AbstractFusionTestCase.php b/Tests/Functional/Fusion/AbstractFusionTestCase.php new file mode 100644 index 0000000..5975f9d --- /dev/null +++ b/Tests/Functional/Fusion/AbstractFusionTestCase.php @@ -0,0 +1,47 @@ +union(FusionSourceCodeCollection::fromFilePath($fusionFile)); + } + $sourceCode = $sourceCode->union(FusionSourceCodeCollection::fromString($fusionSourceCode)); + + $runtime = $this->objectManager->get(RuntimeFactory::class) + ->createFromSourceCode($sourceCode, FusionGlobals::fromArray(['request' => $this->buildActionRequest()])); + + return $runtime->evaluate($fusionPath); + } + + private function buildActionRequest(): ActionRequest + { + $httpRequest = $this->objectManager->get(ServerRequestFactoryInterface::class) + ->createServerRequest('GET', 'http://localhost/'); + + return ActionRequest::fromHttpRequest($httpRequest); + } +} diff --git a/Tests/Functional/Fusion/PaginationArrayTest.php b/Tests/Functional/Fusion/PaginationArrayTest.php new file mode 100644 index 0000000..cb75657 --- /dev/null +++ b/Tests/Functional/Fusion/PaginationArrayTest.php @@ -0,0 +1,277 @@ +assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 100, + currentPage: 1, + + // expected + expectedLinks: [1, 2, 3, '...', 10] + ); + } + + #[Test] + public function middlePageWithGapsOnBothSides(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 100, + currentPage: 5, + + // expected + expectedLinks: [1, '...', 4, 5, 6, '...', 10] + ); + } + + #[Test] + public function firstPageIsLinkedWithoutSeparatorWhenTheRangeStartsAtPageTwo(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 100, + currentPage: 3, + + // expected + expectedLinks: [1, 2, 3, 4, '...', 10] + ); + } + + #[Test] + public function lastPageIsLinkedWithoutSeparatorWhenTheRangeEndsAtTheSecondToLastPage(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 100, + currentPage: 8, + + // expected + expectedLinks: [1, '...', 7, 8, 9, 10] + ); + } + + #[Test] + public function lastPage(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 100, + currentPage: 10, + + // expected + expectedLinks: [1, '...', 8, 9, 10] + ); + } + + #[Test] + public function partiallyFilledLastPageCountsAsPage(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 95, + currentPage: 1, + + // expected + expectedLinks: [1, 2, 3, '...', 10] + ); + } + + #[Test] + public function fewerPagesThanLinks(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 30, + currentPage: 2, + + // expected + expectedLinks: [1, 2, 3] + ); + } + + #[Test] + public function singlePage(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 10, + currentPage: 1, + + // expected + expectedLinks: [1] + ); + } + + #[Test] + public function noLinksWithoutItems(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: false, + + // state + totalCount: 0, + currentPage: 1, + + // expected + expectedLinks: [] + ); + } + + #[Test] + public function previousAndNextLinksOnAMiddlePage(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: true, + + // state + totalCount: 100, + currentPage: 5, + + // expected + expectedLinks: ['previous', 1, '...', 4, 5, 6, '...', 10, 'next'] + ); + } + + #[Test] + public function previousLinkStaysInFrontOfTheFirstPageLink(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: true, + + // state + totalCount: 100, + currentPage: 3, + + // expected + expectedLinks: ['previous', 1, 2, 3, 4, '...', 10, 'next'] + ); + } + + #[Test] + public function noPreviousLinkOnTheFirstPage(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: true, + + // state + totalCount: 100, + currentPage: 1, + + // expected + expectedLinks: [1, 2, 3, '...', 10, 'next'] + ); + } + + #[Test] + public function noNextLinkOnTheLastPage(): void + { + $this->assertPaginationLinks( + // settings + itemsPerPage: 10, + maximumNumberOfLinks: 5, + showPreviousNextLinks: true, + + // state + totalCount: 100, + currentPage: 10, + + // expected + expectedLinks: ['previous', 1, '...', 8, 9, 10] + ); + } + + /** + * @param array $expectedLinks + */ + private function assertPaginationLinks( + // settings + int $itemsPerPage, + int $maximumNumberOfLinks, + bool $showPreviousNextLinks, + // state + int $totalCount, + int $currentPage, + // expected + array $expectedLinks + ): void { + $links = $this->evaluateFusion('pagination', sprintf( + 'pagination = Flowpack.Listable:PaginationArray { + currentPage = %d + totalCount = %d + itemsPerPage = %d + maximumNumberOfLinks = %d + showPreviousNextLinks = %s + }', + $currentPage, + $totalCount, + $itemsPerPage, + $maximumNumberOfLinks, + $showPreviousNextLinks ? 'true' : 'false' + )); + + self::assertSame($expectedLinks, $links); + } +} diff --git a/Tests/Functional/run-tests.sh b/Tests/Functional/run-tests.sh new file mode 100755 index 0000000..b1fd925 --- /dev/null +++ b/Tests/Functional/run-tests.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# +# Runs the functional tests of this package in a throwaway Neos distribution. +# +# Usage: [NEOS_VERSION=9.2] Tests/Functional/run-tests.sh [target-directory] [-- additional phpunit arguments] +# +# The distribution is created on the first run (see setup-test-distribution.sh) and reused afterwards. +# Delete the target directory to start from scratch. +# +# Examples: +# Tests/Functional/run-tests.sh +# Tests/Functional/run-tests.sh /tmp/listable-tests -- --filter PaginationArrayTest +# NEOS_VERSION=9.0 Tests/Functional/run-tests.sh +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +export NEOS_VERSION="${NEOS_VERSION:-9.2}" +TARGET_DIR="${TMPDIR:-/tmp}/flowpack-listable-test-distribution-neos-$NEOS_VERSION" +if [[ $# -gt 0 && "$1" != "--" ]]; then + TARGET_DIR="$1" + shift +fi +if [[ $# -gt 0 && "$1" == "--" ]]; then + shift +fi +TARGET_DIR="${TARGET_DIR%/}" + +if [[ ! -x "$TARGET_DIR/bin/phpunit" ]]; then + "$SCRIPT_DIR/setup-test-distribution.sh" "$TARGET_DIR" +fi + +cd "$TARGET_DIR" +# memory_limit = -1 for PHPUnit and every PHP process it starts (see setup-test-distribution.sh). +# The leading ":" keeps the system's scan dir and adds ours. +export PHP_INI_SCAN_DIR=":$TARGET_DIR/php-ini" +exec bin/phpunit \ + -c Build/BuildEssentials/PhpUnit/FunctionalTests.xml \ + "$@" \ + Packages/Application/Flowpack.Listable/Tests/Functional diff --git a/Tests/Functional/setup-test-distribution.sh b/Tests/Functional/setup-test-distribution.sh new file mode 100755 index 0000000..d34bd64 --- /dev/null +++ b/Tests/Functional/setup-test-distribution.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +# +# Creates a minimal Neos distribution with this package installed (symlinked), to run the functional tests in. +# +# Functional tests boot a full Flow application, so they can't run from the package root alone. +# Used by run-tests.sh and the GitHub CI (.github/workflows/tests.yml). +# +# Usage: [NEOS_VERSION=9.2] Tests/Functional/setup-test-distribution.sh [target-directory] +# +# NEOS_VERSION is the Neos minor version to test against (default: 9.2). The CI tests the lowest and the +# newest supported one. +# +# The target directory must be OUTSIDE of this package: the package is symlinked into it. +set -euo pipefail + +PACKAGE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +NEOS_VERSION="${NEOS_VERSION:-9.2}" +TARGET_DIR="${1:-${TMPDIR:-/tmp}/flowpack-listable-test-distribution-neos-$NEOS_VERSION}" +TARGET_DIR="${TARGET_DIR%/}" + +case "$TARGET_DIR/" in + "$PACKAGE_DIR"/*) + echo "The target directory must be outside of the package directory ($PACKAGE_DIR)." >&2 + exit 1 + ;; +esac + +mkdir -p "$TARGET_DIR" + +cat > "$TARGET_DIR/composer.json" < "$TARGET_DIR/php-ini/memory-limit.ini" + +cd "$TARGET_DIR" +composer update --no-interaction --no-progress diff --git a/composer.json b/composer.json index e460b52..1cd27e1 100755 --- a/composer.json +++ b/composer.json @@ -15,6 +15,11 @@ "Flowpack\\Listable\\": "Classes" } }, + "autoload-dev": { + "psr-4": { + "Flowpack\\Listable\\Tests\\": "Tests" + } + }, "extra": { "applied-flow-migrations": [ "TYPO3.FLOW3-201201261636",