Skip to content

Commit

Permalink
Merge pull request #37 from sitegeist/FEATURE/testing
Browse files Browse the repository at this point in the history
FEATURE: Add unit and functional tests
  • Loading branch information
gradinarufelix authored Apr 25, 2024
2 parents d6a2dd7 + 640cf99 commit 87c3812
Show file tree
Hide file tree
Showing 19 changed files with 1,537 additions and 47 deletions.
121 changes: 86 additions & 35 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,92 @@ name: build

on:
push:
branches:
- 'main'
branches:
- 'main'
pull_request: ~

jobs:
test:
name: "Test (PHP ${{ matrix.php-versions }}, Neos ${{ matrix.neos-versions }})"

strategy:
fail-fast: false
matrix:
php-versions: ['8.1']
neos-versions: ['7.3']
include:
- php-versions: '8.1'
neos-versions: '8.3'
- php-versions: '8.2'
neos-versions: '8.3'
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
with:
path: ${{ env.FLOW_FOLDER }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, xml, json, zlib, iconv, intl, pdo_sqlite
ini-values: date.timezone="Africa/Tunis", opcache.fast_shutdown=0, apc.enable_cli=on

- name: Set Neos Version
run: composer require neos/neos ^${{ matrix.neos-versions }} --no-progress --no-interaction

- name: Run Tests
run: composer test
test:
name: "Test (PHP ${{ matrix.php-versions }}, Neos ${{ matrix.neos-versions }})"

strategy:
fail-fast: false
matrix:
php-versions: ['8.1']
neos-versions: ['7.3', '8.3']
include:
- php-versions: '8.2'
neos-versions: '8.3'
runs-on: ubuntu-latest

env:
FLOW_CONTEXT: Testing
NEOS_FOLDER: neos/
DIST_FOLDER: DistributionPackages/Sitegeist.LostInTranslation

steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, xml, json, zlib, iconv, intl, pdo_sqlite, mysql, pgsql, redis, memcached, memcache, apcu
ini-values: date.timezone="Africa/Tunis", opcache.fast_shutdown=0, apc.enable_cli=on

- name: Install Neos Project and other dependencies
run: |
composer create --no-scripts --no-install neos/neos-base-distribution "${{ env.NEOS_FOLDER }}" "^${{ matrix.neos-versions }}"
cd "${{ env.NEOS_FOLDER }}"
composer require fakerphp/faker "^1.23.0" --no-install --dev
composer require phpstan/phpstan "^1.10.0" --no-install --dev
composer require squizlabs/php_codesniffer "^3.7" --no-install --dev
composer require mockery/mockery "@stable" --no-install --dev
composer config --no-plugins allow-plugins.neos/composer-plugin true
- name: Checkout
uses: actions/checkout@v2
with:
path: ${{ env.NEOS_FOLDER }}${{ env.DIST_FOLDER }}

- name: Finish composer setup
run: |
cd "${{ env.NEOS_FOLDER }}"
composer config repositories.lostintranslation '{ "type": "path", "url": "./${{ env.DIST_FOLDER }}", "options": { "symlink": false } }'
composer require sitegeist/lostintranslation "@dev" --no-install
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: |
~/.cache/composer
~/${{ env.NEOS_FOLDER }}Packages
key: php-${{ matrix.php-versions }}-${{ matrix.dependencies }}${{ hashFiles('**/composer.json') }}
restore-keys: php-${{ matrix.php-versions }}-${{ matrix.dependencies }}

- name: Install dependencies
run: |
cd ${{ env.NEOS_FOLDER }}
composer ${{ matrix.dependencies == 'locked' && 'install' || 'update' }} --no-progress --no-interaction ${{ matrix.dependencies == 'lowest' && '--prefer-lowest' || '' }} ${{ matrix.composer-arguments }}
- name: Set Flow Context
run: echo "FLOW_CONTEXT=${{ env.FLOW_CONTEXT }}" >> $GITHUB_ENV

- name: Run style tests
run: |
cd "${{ env.NEOS_FOLDER }}"
bin/phpcs --colors -n --standard=PSR12 ${{ env.DIST_FOLDER }}/Classes
- name: Run stan tests
run: |
cd "${{ env.NEOS_FOLDER }}"
bin/phpstan analyse ${{ env.DIST_FOLDER }}/Classes
- name: Run unit tests
run: |
cd "${{ env.NEOS_FOLDER }}"
bin/phpunit --colors --stop-on-failure -c ${{ env.DIST_FOLDER }}/Tests/UnitTests.xml --testsuite "LostInTranslation" --verbose
- name: Run functional tests
run: |
cd "${{ env.NEOS_FOLDER }}"
bin/phpunit --colors --stop-on-failure -c ${{ env.DIST_FOLDER }}/Tests/FunctionalTests.xml --testsuite "LostInTranslation" --verbose
20 changes: 18 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
composer.lock
Packages
vendor
/vendor
/Build/Behat/*
/Build/PhpCodesniffer/*
/Build/BuildEssentials
/Build/Reports
/Build/Resources
/Data/
/Packages/
/Web/
/bin/
/Readme.rst
/Upgrading.rst
/flow
/flow.bat
/Tests/Reports
*.example
.phpunit.result.cache
Configuration/README
22 changes: 18 additions & 4 deletions Classes/ContentRepository/NodeTranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Neos\ContentRepository\Domain\Model\Workspace;
use Neos\ContentRepository\Domain\Service\Context;
use Neos\ContentRepository\Domain\Service\ContextFactory;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Service\PublishingService;
use Neos\Neos\Utility\NodeUriPathSegmentGenerator;
Expand Down Expand Up @@ -72,7 +73,7 @@ class NodeTranslationService

/**
* @Flow\Inject
* @var ContextFactory
* @var ContextFactoryInterface
*/
protected $contextFactory;

Expand All @@ -94,6 +95,14 @@ class NodeTranslationService
*/
protected $translatablePropertiesFactory;

/**
* This is an internal property and should always be 'live'.
* Its only purpose is to be overridden in functional testing.
*
* @var string
*/
protected $liveWorkspaceName = 'live';

/**
* @param NodeInterface $node
* @param Context $context
Expand Down Expand Up @@ -135,16 +144,16 @@ public function afterNodePublish(NodeInterface $node, Workspace $workspace): voi
return;
}

if ($workspace->getName() !== 'live') {
if ($workspace->getName() !== $this->liveWorkspaceName) {
return;
}

if ($this->skipAuthorizationChecks) {
$this->securityContext->withoutAuthorizationChecks(function () use ($node) {
$this->syncNode($node);
$this->syncNode($node, $this->liveWorkspaceName);
});
} else {
$this->syncNode($node);
$this->syncNode($node, $this->liveWorkspaceName);
}
}

Expand Down Expand Up @@ -307,4 +316,9 @@ public function syncNode(NodeInterface $sourceNode, string $workspaceName = 'liv
}
}
}

public function resetContextCache(): void
{
$this->contextFirstLevelCache = [];
}
}
10 changes: 10 additions & 0 deletions Configuration/Testing/NodeTypes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'Sitegeist.LostInTranslation.Testing:NodeWithAutomaticTranslation':
superTypes:
'Neos.Neos:Node': true
properties:
inlineEditableStringProperty:
type: string
ui:
inlineEditable: true
stringProperty:
type: string
9 changes: 9 additions & 0 deletions Configuration/Testing/Policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
roles:
'Neos.Flow:Everybody':
privileges:
-
privilegeTarget: 'Neos.Neos:Backend.GeneralAccess'
permission: GRANT
-
privilegeTarget: 'Sitegeist.LostInTranslation:AccessBackendModule'
permission: GRANT
38 changes: 38 additions & 0 deletions Configuration/Testing/Settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Neos:
Flow:
persistence:
backendOptions:
dbname: 'flow_functional_testing'
i18n:
defaultLocale: de
ContentRepository:
contentDimensions:
language:
label: Languages
icon: language
# The default dimension that is applied when creating nodes without specifying a dimension
default: de
# The default preset to use if no URI segment was given when resolving languages in the router
defaultPreset: de
presets:
de:
label: Deutsch
values:
- de
uriSegment: ''
en:
label: English
values:
- en
- de
uriSegment: 'en'
options:
translationStrategy: 'sync'
it:
label: Italiano
values:
- it
- de
uriSegment: 'it'
options:
translationStrategy: 'once'
35 changes: 35 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Testing

This package comes with an extensive automated testing suite, which is automatically run for every
pull request in GitHub.

## CodeSniffer

This tool helps to find and fix code style issues in this package.
To run CodeSniffer tests first ensure that you installed the required
packages using `composer install`. Then run `composer test:style`.


To run the automated fixing of most of the styling issues, you can also execute `composer fix:style`.

## PHPStan

This tool helps to find obvious bugs in your PHP code.
To run PHPStan first ensure that you installed the required
packages using `composer install`. Then run `composer test:stan`.

## Unit Testing

To run unit tests first ensure that you installed the required
packages using `composer install`. Then run `composer test:unit`.

## Functional Testing

To run functional tests on your local machine, install this package in a fresh Neos installation.
Instructions on how to do that can be found here: https://docs.neos.io/guide/installation-development-setup

Once you have done that, you can run the functional tests by executing the following command *in the folder of the Neos installation*:

```shell
FLOW_CONTEXT=Testing bin/phpunit --colors --stop-on-failure -c DistributionPackages/Sitegeist.LostInTranslation/Tests/FunctionalTests.xml --testsuite "LostInTranslation" --verbose
```
10 changes: 10 additions & 0 deletions Tests/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sitegeist\LostInTranslation\Tests\Functional;

use Neos\Flow\Tests\FunctionalTestCase;

abstract class AbstractFunctionalTestCase extends FunctionalTestCase
{
protected static $testablePersistenceEnabled = true;
}
Loading

0 comments on commit 87c3812

Please sign in to comment.