diff --git a/.github/workflows/php-unit-tests.yml b/.github/workflows/php-unit-tests.yml index 7b2600f1c..932babdf1 100644 --- a/.github/workflows/php-unit-tests.yml +++ b/.github/workflows/php-unit-tests.yml @@ -139,17 +139,16 @@ jobs: - name: Install tests run: bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1:${{ job.services.mysql.ports['3306'] }} ${{ matrix.wp }} true - - name: Run tests if: ${{ !matrix.webp }} - run: composer run test:no-cov + run: composer run test env: WP_MULTISITE: ${{ matrix.multisite }} - name: Run tests with coverage if: matrix.coverage == true run: | - composer run test:codecov + composer run test:integration:codecov - name: Upload coverage results to Coveralls if: matrix.coverage == true diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index f6fd33636..e87e7f9a9 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -14,7 +14,7 @@ SKIP_DB_CREATE=${6-false} TMPDIR=${TMPDIR-/tmp} TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") -TMPDIR=$(realpath $TMPDIR) +# TMPDIR=$(realpath $TMPDIR) WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress} diff --git a/composer.json b/composer.json index 8ed46e789..f86ba8cd7 100644 --- a/composer.json +++ b/composer.json @@ -120,9 +120,17 @@ "@test", "@cs" ], - "test": "phpunit", - "test:codecov": "phpunit --coverage-clover ./build/logs/clover.xml", - "test:make-pot": "wp i18n make-pot src tests/languages/timber.pot --domain= && wp i18n make-pot ./tests/assets/translations ./tests/languages/timber-test.pot --domain=timber-test", - "test:no-cov": "phpunit --no-coverage" + "test": [ + "@test:integration:acf", + "@test:integration:coauthors-plus", + "@test:integration:wpml", + "@test:integration" + ], + "test:integration": "phpunit --no-coverage --exclude-group acf,coauthors-plus,wpml", + "test:integration:acf": "phpunit --no-coverage --group acf", + "test:integration:coauthors-plus": "phpunit --no-coverage --group coauthors-plus", + "test:integration:codecov": "phpunit --coverage-clover ./build/logs/clover.xml", + "test:integration:wpml": "phpunit --no-coverage --group wpml", + "test:make-pot": "wp i18n make-pot src tests/languages/timber.pot --domain= && wp i18n make-pot ./tests/assets/translations ./tests/languages/timber-test.pot --domain=timber-test" } } diff --git a/src/CoreEntity.php b/src/CoreEntity.php index 8c4b1dbea..158508768 100644 --- a/src/CoreEntity.php +++ b/src/CoreEntity.php @@ -177,7 +177,7 @@ protected function fetch_meta($field_name = '', $args = [], $apply_filters = tru $object_meta = \get_metadata($object_type, $this->ID, $field_name, true); // Mimick $single argument when fetching all meta values. - if (empty($field_name) && \is_array($object_meta) && !empty($object_meta)) { + if (empty($field_name) && \is_array($object_meta)) { $object_meta = \array_map(function ($meta) { /** * We use array_key_exists() instead of isset(), because when the meta value is null, isset() would @@ -192,11 +192,6 @@ protected function fetch_meta($field_name = '', $args = [], $apply_filters = tru return $meta; }, $object_meta); } - - // Empty result. - if (empty($object_meta)) { - $object_meta = empty($field_name) ? [] : null; - } } if ($apply_filters) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f361bbdd8..aa06ac572 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -14,35 +14,66 @@ // Get access to tests_add_filter() function. require_once $_tests_dir . '/includes/functions.php'; -/** - * Callback to manually load the plugin - */ -function _manually_load_plugin() +function tt_get_arg(string $key) { - Timber\Timber::init(); + foreach ($_SERVER['argv'] as $index => $arg) { + if ($key === substr($arg, 0, strlen($key))) { + return [ + 'index' => $index, + $key => str_replace("{$key}=", '', $arg), + ]; + } + } + return false; +} + +function tt_is_group(string $group_name) +{ + $group = tt_get_arg('--group'); + if (false === $group) { + return false; + } + + $group_name_index = ++$group['index']; - require dirname(__FILE__) . '/../wp-content/plugins/advanced-custom-fields/acf.php'; - if (file_exists(dirname(__FILE__) . '/../wp-content/plugins/co-authors-plus/co-authors-plus.php')) { - include dirname(__FILE__) . '/../wp-content/plugins/co-authors-plus/co-authors-plus.php'; + if (!isset($_SERVER['argv'][$group_name_index])) { + return false; } + + return ($group_name === $_SERVER['argv'][$group_name_index]); } // Add plugin to active mu-plugins to make sure it gets loaded. -tests_add_filter('muplugins_loaded', '_manually_load_plugin'); +tests_add_filter('muplugins_loaded', function () { + // Load Timber + Timber\Timber::init(); -// WPML integration -define('ICL_LANGUAGE_CODE', 'en'); + if (tt_is_group('acf')) { + require __DIR__ . '/../wp-content/plugins/advanced-custom-fields/acf.php'; + } -/** - * Mocked function for testing menus in WPML - */ -function wpml_object_id_filter($element_id, $element_type = 'post', $return_original_if_missing = false, $language_code = null) -{ - $locations = get_nav_menu_locations(); - if (isset($locations['extra-menu'])) { - return $locations['extra-menu']; + if (tt_is_group('coauthors-plus')) { + require __DIR__ . '/../wp-content/plugins/co-authors-plus/co-authors-plus.php'; + } + + if (tt_is_group('wpml')) { + // WPML integration + define('ICL_LANGUAGE_CODE', 'en'); + } +}); + +if (tt_is_group('wpml')) { + /** + * Mocked function for testing menus in WPML + */ + function wpml_object_id_filter($element_id, $element_type = 'post', $return_original_if_missing = false, $language_code = null) + { + $locations = get_nav_menu_locations(); + if (isset($locations['extra-menu'])) { + return $locations['extra-menu']; + } + return $element_id; } - return $element_id; } /** diff --git a/tests/test-timber-cache.php b/tests/test-timber-cache.php index 37ec0f340..70c2cef7b 100644 --- a/tests/test-timber-cache.php +++ b/tests/test-timber-cache.php @@ -329,7 +329,7 @@ public function testTimberLoaderCacheObject() $clear = $loader->clear_cache_timber(Timber\Loader::CACHE_OBJECT); $this->assertTrue($clear); $works = true; - + if (isset($wp_object_cache->cache[Timber\Loader::CACHEGROUP]) && !empty($wp_object_cache->cache[Timber\Loader::CACHEGROUP])) { $works = false; diff --git a/tests/test-timber-image-resize.php b/tests/test-timber-image-resize.php index 21d09adb4..c3fe7213c 100644 --- a/tests/test-timber-image-resize.php +++ b/tests/test-timber-image-resize.php @@ -180,56 +180,4 @@ public function testSideloadedResize() $this->assertEquals($expected, $sideloaded); } - - public function testWPMLurlRemote() - { - // this test replicates the url issue caused by the WPML language identifier in the url - // However, WPML can't be installed with composer so this test mocks the WPML plugin - - // define ICL_LANGUAGE_CODE constant because on subsequent calls (i.e. if downloaded file exists), - // replacement done in Integrations/WPML.php will fail and trigger an error here - if (!defined('ICL_LANGUAGE_CODE')) { - define('ICL_LANGUAGE_CODE', 'en'); - } - - // WPML uses a filter to alter the home_url - $home_url_filter = function ($url) { - return $url . '/en'; - }; - add_filter('home_url', $home_url_filter, -10, 4); - - $img = 'https://raw.githubusercontent.com/timber/timber/master/tests/assets/arch-2night.jpg'; - // test with a local and external file - $resized = Timber\ImageHelper::resize($img, 50, 50); - - // make sure the base url has not been duplicated (https://github.com/timber/timber/issues/405) - $this->assertLessThanOrEqual(1, substr_count($resized, 'example.org')); - // make sure the image has been resized - $resized = Timber\URLHelper::url_to_file_system($resized); - $this->assertTrue(TestTimberImage::checkSize($resized, 50, 50), 'image should be resized'); - } - - public function testWPMLurlLocal() - { - // this test replicates the url issue caused by the WPML language identifier in the url - // However, WPML can't be installed with composer so this test mocks the WPML plugin - - // WPML uses a filter to alter the home_url - $home_url_filter = function ($url) { - return $url . '/en'; - }; - add_filter('home_url', $home_url_filter, -10, 4); - - // test with a local and external file - $img = 'arch.jpg'; - $img = TestTimberImage::copyTestAttachment($img); - - $resized = Timber\ImageHelper::resize($img, 50, 50); - - // make sure the base url has not been duplicated (https://github.com/timber/timber/issues/405) - $this->assertLessThanOrEqual(1, substr_count($resized, 'example.org')); - // make sure the image has been resized - $resized = Timber\URLHelper::url_to_file_system($resized); - $this->assertTrue(TestTimberImage::checkSize($resized, 50, 50), 'image should be resized'); - } } diff --git a/tests/test-timber-integration-acf.php b/tests/test-timber-integration-acf.php index 2444ff290..8626203d2 100644 --- a/tests/test-timber-integration-acf.php +++ b/tests/test-timber-integration-acf.php @@ -3,6 +3,7 @@ use Timber\User; /** + * @group acf * @group users-api * @group comments-api * @group integrations @@ -10,6 +11,14 @@ */ class TestTimberIntegrationACF extends Timber_UnitTestCase { + public function setUp(): void + { + if (!function_exists('get_field')) { + $this->markTestSkipped('ACF plugin is not loaded'); + } + parent::setUp(); + } + public function testACFGetFieldPost() { $post_id = $this->factory->post->create(); @@ -462,6 +471,38 @@ public function testACFObjectTransformsDoesNotTriggerNotice() $this->assertSame(false, $file); } + /** + * @ticket #824 + */ + public function testTermWithNativeMetaNotExisting() + { + $tid = $this->factory->term->create([ + 'name' => 'News', + 'taxonomy' => 'category', + ]); + + add_term_meta($tid, 'bar', 'qux'); + ; + $wp_native_value = get_term_meta($tid, 'foo', true); + $acf_native_value = get_field('foo', 'category_' . $tid); + + $valid_wp_native_value = get_term_meta($tid, 'bar', true); + $valid_acf_native_value = get_field('bar', 'category_' . $tid); + + $term = Timber::get_term($tid); + + //test baseline "bar" data + $this->assertEquals('qux', $valid_wp_native_value); + $this->assertEquals('qux', $valid_acf_native_value); + $this->assertEquals('qux', $term->bar); + + //test the one that doesn't exist + $this->assertEquals('string', gettype($wp_native_value)); + $this->assertEmpty($wp_native_value); + $this->assertNull($acf_native_value); + $this->assertNotTrue($term->meta('foo')); + } + private function register_field($field_name, $field_type, $field_args = []) { $group_key = sprintf('group_%s', uniqid()); diff --git a/tests/test-timber-integration-wpml.php b/tests/test-timber-integration-wpml.php index 512d89216..16f995bd1 100644 --- a/tests/test-timber-integration-wpml.php +++ b/tests/test-timber-integration-wpml.php @@ -6,6 +6,14 @@ */ class TestTimberIntegrationWPML extends Timber_UnitTestCase { + public function setUp(): void + { + if (!defined('ICL_LANGUAGE_CODE')) { + $this->markTestSkipped('WPML plugin is not loaded'); + } + parent::setUp(); + } + public function testFileSystemToURLWithWPML() { $this->add_filter_temporarily('home_url', function ($url, $path) { diff --git a/tests/test-timber-integrations-coauthors.php b/tests/test-timber-integrations-coauthors.php index d757cd565..935f33240 100644 --- a/tests/test-timber-integrations-coauthors.php +++ b/tests/test-timber-integrations-coauthors.php @@ -5,6 +5,7 @@ /** * @group posts-api * @group integrations + * @group coauthors-plus */ class TestTimberIntegrationsCoAuthors extends Timber_UnitTestCase { @@ -20,12 +21,12 @@ public function expectedDeprecated() parent::expectedDeprecated(); } - public function set_up() + public function setUp(): void { if (!class_exists('CoAuthors_Plus')) { - return $this->markTestSkipped('CoAuthors_Plus plugin not loaded'); + $this->markTestSkipped('CoAuthors Plus plugin is not loaded'); } - parent::set_up(); + parent::setUp(); } /* ---------------- diff --git a/tests/test-timber-menu.php b/tests/test-timber-menu.php index 18b80e233..26f050674 100644 --- a/tests/test-timber-menu.php +++ b/tests/test-timber-menu.php @@ -636,7 +636,7 @@ public function testMenuMetaSet() $this->assertEquals('bar', $item->foo); $this->assertNotEquals('bar', $item->meta('foo')); $this->assertEquals('stardust', $item->meta('ziggy')); - $this->assertNull($item->meta('asdfafds')); + $this->assertSame('', $item->meta('asdfafds')); } public function testMenuMeta() diff --git a/tests/test-timber-meta.php b/tests/test-timber-meta.php index 1a2b04161..cee0a9d86 100644 --- a/tests/test-timber-meta.php +++ b/tests/test-timber-meta.php @@ -97,10 +97,10 @@ public function testMetaReturnsNullWhenResultIsEmpty() $user = Timber::get_user($user_id); $comment = Timber::get_comment($comment_id); - $this->assertSame(null, $post->meta('not_found')); - $this->assertSame(null, $term->meta('not_found')); - $this->assertSame(null, $user->meta('not_found')); - $this->assertSame(null, $comment->meta('not_found')); + $this->assertSame('', $post->meta('not_found')); + $this->assertSame('', $term->meta('not_found')); + $this->assertSame('', $user->meta('not_found')); + $this->assertSame('', $comment->meta('not_found')); } public function testPreMetaFilter() @@ -375,16 +375,16 @@ public function testRawMetaInexistent() ] ); - $this->assertSame(null, $post->raw_meta('my_custom_property_inexistent')); + $this->assertSame('', $post->raw_meta('my_custom_property_inexistent')); $this->assertSame('', $post_string); - $this->assertSame(null, $term->raw_meta('my_custom_property_inexistent')); + $this->assertSame('', $term->raw_meta('my_custom_property_inexistent')); $this->assertSame('', $term_string); - $this->assertSame(null, $user->raw_meta('my_custom_property_inexistent')); + $this->assertSame('', $user->raw_meta('my_custom_property_inexistent')); $this->assertSame('', $user_string); - $this->assertSame(null, $comment->raw_meta('my_custom_property_inexistent')); + $this->assertSame('', $comment->raw_meta('my_custom_property_inexistent')); $this->assertSame('', $comment_string); } diff --git a/tests/test-timber-post.php b/tests/test-timber-post.php index b527f6412..4c52146fc 100644 --- a/tests/test-timber-post.php +++ b/tests/test-timber-post.php @@ -340,7 +340,7 @@ public function testCustomFieldExcerptRevision() $post_id = $this->factory->post->create([ 'post_author' => 5, ]); - update_field('test_field', 'The custom field content', $post_id); + update_post_meta($post_id, 'test_field', 'The custom field content'); $assertCustomFieldVal = 'This has been revised'; $revision_id = $this->factory->post->create([ @@ -348,7 +348,7 @@ public function testCustomFieldExcerptRevision() 'post_status' => 'inherit', 'post_parent' => $post_id, ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); + update_post_meta($revision_id, 'test_field', $assertCustomFieldVal); $uid = $this->factory->user->create([ 'user_login' => 'timber', @@ -373,48 +373,6 @@ public function testCustomFieldExcerptRevision() $this->assertEquals($assertCustomFieldVal, $str_getfield); } - public function testCustomFieldExcerptNotRevision() - { - global $current_user; - global $wp_query; - $original_content = 'The custom field content'; - - $post_id = $this->factory->post->create([ - 'post_author' => 5, - ]); - update_field('test_field', $original_content, $post_id); - - $assertCustomFieldVal = 'This has been revised'; - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - $user->add_role('administrator'); - - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - - $post = Timber::get_post($post_id); - - $str_direct = Timber::compile_string('{{post.test_field}}', [ - 'post' => $post, - ]); - $str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [ - 'post' => $post, - ]); - - $this->assertEquals($original_content, $str_direct); - $this->assertEquals($original_content, $str_getfield); - } - public function testContent() { $quote = 'The way to do well is to do well.'; diff --git a/tests/test-timber-revisions.php b/tests/test-timber-revisions.php index e34486bfe..dd03b30ed 100644 --- a/tests/test-timber-revisions.php +++ b/tests/test-timber-revisions.php @@ -290,7 +290,7 @@ public function testCustomFieldPreviewRevisionMethod() $post_id = $this->factory->post->create([ 'post_author' => 5, ]); - update_field('test_field', 'The custom field content', $post_id); + update_post_meta($post_id, 'test_field', 'The custom field content'); $assertCustomFieldVal = 'This has been revised'; $revision_id = $this->factory->post->create([ @@ -298,7 +298,7 @@ public function testCustomFieldPreviewRevisionMethod() 'post_status' => 'inherit', 'post_parent' => $post_id, ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); + update_post_meta($revision_id, 'test_field', $assertCustomFieldVal); $uid = $this->factory->user->create([ 'user_login' => 'timber', @@ -326,7 +326,7 @@ public function testCustomFieldPreviewRevisionImported() $post_id = $this->factory->post->create([ 'post_author' => 5, ]); - update_field('test_field', 'The custom field content', $post_id); + update_post_meta($post_id, 'test_field', 'The custom field content'); $assertCustomFieldVal = 'This has been revised'; $revision_id = $this->factory->post->create([ @@ -334,7 +334,7 @@ public function testCustomFieldPreviewRevisionImported() 'post_status' => 'inherit', 'post_parent' => $post_id, ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); + update_post_meta($revision_id, 'test_field', $assertCustomFieldVal); $uid = $this->factory->user->create([ 'user_login' => 'timber', @@ -354,47 +354,6 @@ public function testCustomFieldPreviewRevisionImported() $this->assertEquals($assertCustomFieldVal, $str_direct); } - public function testCustomFieldPreviewNotRevision() - { - global $current_user; - global $wp_query; - $original_content = 'The custom field content'; - - $post_id = $this->factory->post->create([ - 'post_author' => 5, - ]); - update_field('test_field', $original_content, $post_id); - - $assertCustomFieldVal = 'This has been revised'; - $revision_id = $this->factory->post->create([ - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'post_parent' => $post_id, - ]); - update_field('test_field', $assertCustomFieldVal, $revision_id); - - $uid = $this->factory->user->create([ - 'user_login' => 'timber', - 'user_pass' => 'timber', - ]); - $user = wp_set_current_user($uid); - $user->add_role('administrator'); - - $wp_query->queried_object_id = $post_id; - $wp_query->queried_object = get_post($post_id); - $post = Timber::get_post($post_id); - - $str_direct = Timber::compile_string('{{post.test_field}}', [ - 'post' => $post, - ]); - $str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [ - 'post' => $post, - ]); - - $this->assertEquals($original_content, $str_direct); - $this->assertEquals($original_content, $str_getfield); - } - /** * Tests whether visiting a post revision with an attachment/featured image doesn’t throw a fatal error. * diff --git a/tests/test-timber-term.php b/tests/test-timber-term.php index 8dcaeae87..ad9522ba7 100644 --- a/tests/test-timber-term.php +++ b/tests/test-timber-term.php @@ -589,38 +589,6 @@ public function testTermWithNativeMetaFalse() $this->assertSame('', $term->meta('foo')); } - /** - * @ticket #824 - */ - public function testTermWithNativeMetaNotExisting() - { - $tid = $this->factory->term->create([ - 'name' => 'News', - 'taxonomy' => 'category', - ]); - - add_term_meta($tid, 'bar', 'qux'); - ; - $wp_native_value = get_term_meta($tid, 'foo', true); - $acf_native_value = get_field('foo', 'category_' . $tid); - - $valid_wp_native_value = get_term_meta($tid, 'bar', true); - $valid_acf_native_value = get_field('bar', 'category_' . $tid); - - $term = Timber::get_term($tid); - - //test baseline "bar" data - $this->assertEquals('qux', $valid_wp_native_value); - $this->assertEquals('qux', $valid_acf_native_value); - $this->assertEquals('qux', $term->bar); - - //test the one that doesn't exist - $this->assertEquals('string', gettype($wp_native_value)); - $this->assertEmpty($wp_native_value); - $this->assertNull($acf_native_value); - $this->assertNotTrue($term->meta('foo')); - } - public function testEditLink() { wp_set_current_user(1); diff --git a/tests/test-timber-url-helper.php b/tests/test-timber-url-helper.php index 8be9de603..3b9fefe63 100644 --- a/tests/test-timber-url-helper.php +++ b/tests/test-timber-url-helper.php @@ -64,22 +64,6 @@ public function testStartsWithHTTPsFlip() $this->assertFalse(Timber\URLHelper::starts_with($haystack, $nope)); } - public function testFileSystemToURLWithWPML() - { - self::_setLanguage(); - add_filter('home_url', [$this, 'addWPMLHomeFilterForRegExTest'], 10, 2); - $image = TestTimberImage::copyTestAttachment(); - - $url = Timber\URLHelper::file_system_to_url($image); - $this->assertStringEndsWith('://example2.org/wp-content/uploads/' . date('Y/m') . '/arch.jpg', $url); - remove_filter('home_url', [$this, 'addWPMLHomeFilterForRegExTest']); - } - - public function addWPMLHomeFilterForRegExTest($url, $path) - { - return 'http://example2.org/en' . $path; - } - public function testFileSystemToURL() { $image = TestTimberImage::copyTestAttachment(); @@ -87,33 +71,6 @@ public function testFileSystemToURL() $this->assertStringEndsWith('://example.org/wp-content/uploads/' . date('Y/m') . '/arch.jpg', $url); } - public function addWPMLHomeFilter($url, $path) - { - return 'http://example.org/en' . $path; - } - - public function _setLanguage() - { - if (!defined('ICL_LANGUAGE_CODE')) { - define('ICL_LANGUAGE_CODE', 'en'); - } - } - - public function _setupWPMLDirectory() - { - self::_setLanguage(); - add_filter('home_url', [$this, 'addWPMLHomeFilter'], 10, 2); - } - - public function testFileSystemToURLWithWPMLPrefix() - { - self::_setupWPMLDirectory(); - $image = TestTimberImage::copyTestAttachment(); - $url = Timber\URLHelper::file_system_to_url($image); - $this->assertEquals('http://example.org/wp-content/uploads/' . date('Y/m') . '/arch.jpg', $url); - remove_filter('home_url', [$this, 'addWPMLHomeFilter']); - } - public function testContentSubDirectory() { $subdir = Timber\URLHelper::get_content_subdir();