Skip to content

Commit

Permalink
fix: tests failing since Twig 3.8.0 (#2895)
Browse files Browse the repository at this point in the history
Twig recently changed the way exceptions are thrown during template rendering. Depending on the version, different types of execption can be thrown.

See: twigphp/Twig@85bf01b

fix: failing tests for WP <6.4

Add two new methods in base test case to check WordPress version and mark test

Co-authored-by: Erik van der Bas <erik@basedonline.nl>
  • Loading branch information
nlemoine and Levdbas committed Jan 26, 2024
1 parent 664ea62 commit f4a233e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bin/install-wp-tests.sh
Expand Up @@ -92,7 +92,7 @@ install_wp() {
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

install_test_suite() {
Expand Down
12 changes: 12 additions & 0 deletions tests/Timber_UnitTestCase.php
Expand Up @@ -262,4 +262,16 @@ public function restore_themes()
wp_clean_themes_cache();
unset($GLOBALS['wp_themes']);
}

public function isWordPressVersion(string $version, string $operator = '=')
{
return version_compare($GLOBALS['wp_version'], $version, $operator);
}

public function skipForWordpressVersion(string $version, string $operator = '<')
{
if ($this->isWordPressVersion($version, $operator)) {
$this->markTestSkipped("This test requires WordPress version $version or higher.");
}
}
}
48 changes: 44 additions & 4 deletions tests/test-timber-meta.php
Expand Up @@ -2,6 +2,8 @@

use Timber\Integration\AcfIntegration;
use Timber\Timber;
use Twig\Environment;
use Twig\Error\RuntimeError;

/**
* Class TestTimberMeta
Expand Down Expand Up @@ -618,7 +620,17 @@ public function testMetaDirectAccessProtectedMethodConflict()
*/
public function testPostMetaDirectAccessMethodWithRequiredParametersConflict()
{
$this->expectException(\ArgumentCountError::class);
/**
* Twig 3.8 changed the way some exception are handled and a different exception is thrown.
*
* @see https://github.com/twigphp/Twig/commit/85bf01b4abd4b4ee6f6d1aca19af74189c939d69
*/
if (version_compare(Environment::VERSION, '3.8.0', '>=')) {
$this->expectException(RuntimeError::class);
} else {
$this->expectException(\ArgumentCountError::class);
}

$post_id = $this->factory->post->create();

update_post_meta($post_id, 'public_method_with_args', 'I am a meta value');
Expand All @@ -641,7 +653,17 @@ public function testPostMetaDirectAccessMethodWithRequiredParametersConflict()
*/
public function testTermMetaDirectAccessMethodWithRequiredParametersConflict()
{
$this->expectException(\ArgumentCountError::class);
/**
* Twig 3.8 changed the way some exceptions are handled and a different exception is thrown.
*
* @see https://github.com/twigphp/Twig/commit/85bf01b4abd4b4ee6f6d1aca19af74189c939d69
*/
if (version_compare(Environment::VERSION, '3.8.0', '>=')) {
$this->expectException(RuntimeError::class);
} else {
$this->expectException(\ArgumentCountError::class);
}

$term_id = $this->factory->term->create();

update_term_meta($term_id, 'public_method_with_args', 'I am a meta value');
Expand All @@ -666,7 +688,16 @@ public function testTermMetaDirectAccessMethodWithRequiredParametersConflict()
*/
public function testUserMetaDirectAccessMethodWithRequiredParametersConflict()
{
$this->expectException(\ArgumentCountError::class);
/**
* Twig 3.8 changed the way some exceptions are handled and a different exception is thrown.
*
* @see https://github.com/twigphp/Twig/commit/85bf01b4abd4b4ee6f6d1aca19af74189c939d69
*/
if (version_compare(Environment::VERSION, '3.8.0', '>=')) {
$this->expectException(RuntimeError::class);
} else {
$this->expectException(\ArgumentCountError::class);
}

$user_id = $this->factory->user->create();

Expand All @@ -693,7 +724,16 @@ public function testUserMetaDirectAccessMethodWithRequiredParametersConflict()
*/
public function testCommentMetaDirectAccessMethodWithRequiredParametersConflict()
{
$this->expectException(\ArgumentCountError::class);
/**
* Twig 3.8 changed the way some exceptions are handled and a different exception is thrown.
*
* @see https://github.com/twigphp/Twig/commit/85bf01b4abd4b4ee6f6d1aca19af74189c939d69
*/
if (version_compare(Environment::VERSION, '3.8.0', '>=')) {
$this->expectException(RuntimeError::class);
} else {
$this->expectException(\ArgumentCountError::class);
}

$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create([
Expand Down
37 changes: 19 additions & 18 deletions tests/test-timber-wp-functions.php
Expand Up @@ -12,11 +12,12 @@ public function testFunctionFire()
$this->assertEquals('jared sez hi', $output);
}

/**
* @expectedDeprecated the_block_template_skip_link
*/
public function testFooterOnFooterFW()
{
if ($this->isWordPressVersion('6.4', '>=')) {
$this->setExpectedDeprecated('the_block_template_skip_link');
}

global $wp_scripts;
$wp_scripts = null;
wp_enqueue_script('jquery', false, [], false, true);
Expand All @@ -30,11 +31,11 @@ public function testFooterOnFooterFW()
$this->assertSame(0, strlen($wp_footer_output1));
}

/**
* @expectedDeprecated the_block_template_skip_link
*/
public function testFooterAlone()
{
if ($this->isWordPressVersion('6.4', '>=')) {
$this->setExpectedDeprecated('the_block_template_skip_link');
}
global $wp_scripts;
$wp_scripts = null;
wp_enqueue_script('jquery', false, [], false, true);
Expand All @@ -53,11 +54,11 @@ public function testDoubleAction()
$this->assertEquals('bar', $fw1->call());
}

/**
* @expectedDeprecated the_block_template_skip_link
*/
public function testDoubleActionWPFooter()
{
if ($this->isWordPressVersion('6.4', '>=')) {
$this->setExpectedDeprecated('the_block_template_skip_link');
}
global $wp_scripts;
$wp_scripts = null;
add_action('wp_footer', 'echo_junk');
Expand All @@ -69,11 +70,11 @@ public function testDoubleActionWPFooter()
remove_action('wp_footer', 'echo_junk');
}

/**
* @expectedDeprecated the_block_template_skip_link
*/
public function testInTwig()
{
if ($this->isWordPressVersion('6.4', '>=')) {
$this->setExpectedDeprecated('the_block_template_skip_link');
}
global $wp_scripts;
$wp_scripts = null;
wp_enqueue_script('jquery', false, [], false, true);
Expand All @@ -82,11 +83,11 @@ public function testInTwig()
$this->assertGreaterThan(-1, $pos);
}

/**
* @expectedDeprecated the_block_template_skip_link
*/
public function testInTwigString()
{
if ($this->isWordPressVersion('6.4', '>=')) {
$this->setExpectedDeprecated('the_block_template_skip_link');
}
global $wp_scripts;
$wp_scripts = null;
wp_enqueue_script('jquery', false, [], false, true);
Expand All @@ -95,11 +96,11 @@ public function testInTwigString()
$this->assertGreaterThan(-1, $pos);
}

/**
* @expectedDeprecated the_block_template_skip_link
*/
public function testAgainstFooterFunctionOutput()
{
if ($this->isWordPressVersion('6.4', '>=')) {
$this->setExpectedDeprecated('the_block_template_skip_link');
}
global $wp_scripts;
$wp_scripts = null;
wp_enqueue_script('colorpicker', false, [], false, true);
Expand Down

0 comments on commit f4a233e

Please sign in to comment.