Skip to content

Commit

Permalink
Merge pull request #181 from Automattic/refresh
Browse files Browse the repository at this point in the history
Refresh tests, up minimum WP version
  • Loading branch information
GaryJones authored Feb 8, 2024
2 parents 713f950 + 3069b1f commit c494c3a
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 363 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cs-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "7.4"
php-version: "8.3"
coverage: none
tools: cs2pr

Expand All @@ -37,7 +37,7 @@ jobs:
uses: korelstar/xmllint-problem-matcher@v1

- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

# Validate the composer.json file.
# @link https://getcomposer.org/doc/03-cli.md#validate
Expand All @@ -49,7 +49,7 @@ jobs:
- name: Install PHP Dependencies with Composer
uses: ramsey/[email protected]
# Lint PHP.

- name: Lint PHP against parse errors
run: composer lint-ci | cs2pr

Expand Down
46 changes: 19 additions & 27 deletions .github/workflows/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,36 @@ on:
jobs:
test:
name: WP ${{ matrix.wordpress }} on PHP ${{ matrix.php }}
# Ubuntu-20.x includes MySQL 8.0, which causes `caching_sha2_password` issues with PHP < 7.4
# https://www.php.net/manual/en/mysqli.requirements.php
# TODO: change to ubuntu-latest when we no longer support PHP < 7.4
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.allowed_failure }}

env:
WP_VERSION: ${{ matrix.wordpress }}

strategy:
matrix:
wordpress: ["5.5", "5.6", "5.7"]
php: ["7.4", "8.0", "8.1"]
wordpress: [ '5.9', '6.4' ]
php: [ '7.4', '8.3' ]
allowed_failure: [ false ]
include:
- php: "8.0"
# Ignore platform requirements, so that PHPUnit 7.5 can be installed on PHP 8.0 (and above).
composer-options: "--ignore-platform-reqs"
# Check upcoming WP.
- php: '8.3'
wordpress: 'trunk'
allowed_failure: true
# Check upcoming PHP.
# - php: '8.4'
# wordpress: 'latest'
# allowed_failure: true
- php: '8.2'
extensions: pcov
ini-values: pcov.directory=., "pcov.exclude=\"~(vendor|tests)~\""
coverage: pcov
exclude:
- php: "8.0"
wordpress: "5.5"
allowed_failure: false
fail-fast: false

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
Expand All @@ -52,22 +55,11 @@ jobs:
- name: Setup problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

# Setup PCOV since we're using PHPUnit < 8 which has it integrated. Requires PHP 7.1.
# Ignore platform reqs to make it install on PHP 8.
# https://github.com/krakjoe/pcov-clobber
- name: Setup PCOV
if: ${{ matrix.php == 8.0 }}
run: |
composer require pcov/clobber --ignore-platform-reqs
vendor/bin/pcov clobber
- name: Setup Problem Matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install Composer dependencies
uses: ramsey/composer-install@v1
with:
composer-options: "${{ matrix.composer-options }}"
uses: ramsey/composer-install@v2

- name: Start MySQL Service
run: sudo systemctl start mysql.service
Expand All @@ -76,10 +68,10 @@ jobs:
run: composer prepare-ci

- name: Run integration tests (single site)
if: ${{ matrix.php != 8.0 }}
if: ${{ matrix.php != 8.2 }}
run: composer test
- name: Run integration tests (single site with code coverage)
if: ${{ matrix.php == 8.0 }}
if: ${{ matrix.php == 8.2 }}
run: composer coverage-ci
- name: Run integration tests (multisite)
run: composer test-ms
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
/vendor
/.phpunit.cache/
/composer.lock
/vendor/
2 changes: 1 addition & 1 deletion .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<rule ref="WordPress-Docs"/>
<!-- For help in understanding these custom sniff properties:
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<config name="minimum_supported_wp_version" value="5.5"/>
<config name="minimum_supported_wp_version" value="5.9"/>

<!-- Rules: WordPress VIP - see
https://github.com/Automattic/VIP-Coding-Standards -->
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3.5",
"wp-coding-standards/wpcs": "^2.3.0",
"yoast/phpunit-polyfills": "^2.0"
"yoast/wp-test-utils": "^1.2.0"
},
"autoload-dev": {
"psr-4": {
"Automattic\\MSM_Sitemap\\Tests\\": "tests/"
}
},
"scripts": {
"coverage": [
"@php ./vendor/bin/phpunit --coverage-html ./build/coverage-html"
"@php ./vendor/bin/phpunit --coverage-html ./.phpunit.cache/code-coverage-html"
],
"coverage-ci": [
"@php ./vendor/bin/phpunit"
Expand All @@ -47,7 +52,7 @@
"bash bin/install-wp-tests.sh wordpress_test root root localhost"
],
"test": [
"@php ./vendor/bin/phpunit --testsuite WP_Tests"
"@php ./vendor/bin/phpunit --testsuite WP_Tests --no-coverage"
],
"test-ms": [
"@putenv WP_MULTISITE=1",
Expand Down
39 changes: 29 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
<phpunit
bootstrap="./tests/bootstrap.php"
backupGlobals="false"
colors="true"
>
<testsuites>
<testsuite name="WP_Tests">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
colors="true"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="WP_Tests">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<file>msm-sitemap.php</file>
<directory>templates</directory>
<directory suffix=".php">includes</directory>
</include>
</coverage>
</phpunit>
64 changes: 30 additions & 34 deletions tests/test-sitemap-cpt.php → tests/CPTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,28 @@
* @package Metro_Sitemap/unit_tests
*/

require_once( 'msm-sitemap-test.php' );
namespace Automattic\MSM_Sitemap\Tests;

use Metro_Sitemap;

/**
* Unit Tests to confirm Sitemaps are properly handled for Custom Post Types
*
* @author Matthew Denton (mdbitz)
*/
class WP_Test_Sitemap_CPT extends WP_UnitTestCase {

/**
* Base Test Class Instance
*
* @var MSM_SIteMap_Test
*/
private $test_base;
class CPTTest extends TestCase {

/**
* Custom Post Type
*
* @var str
* @var string
*/
const TEST_CPT = 'msm_stest';
public const TEST_CPT = 'msm_stest';

/**
* Initialize Test CPT
*/
static function setupBeforeClass(): void {
public static function setupBeforeClass(): void {
register_post_type(
self::TEST_CPT,
array(
Expand All @@ -55,56 +50,54 @@ static function setupBeforeClass(): void {
/**
* Initialize MSM_SiteMap_Test
*/
function setup(): void {

$this->test_base = new MSM_SiteMap_Test();

// Create Multiple Posts acorss various Dates.
public function setup(): void {
// Create Multiple Posts across various Dates.
$date = time();

// 3 for Today, 1 in "draft" status
$cur_day = date( 'Y', $date ) . '-' . date( 'm', $date ) . '-' . date( 'd', $date ) . ' 00:00:00';
$this->test_base->create_dummy_post( $cur_day );
$this->test_base->create_dummy_post( $cur_day );
$this->test_base->create_dummy_post( $cur_day, 'draft' );
$this->create_dummy_post( $cur_day );
$this->create_dummy_post( $cur_day );
$this->create_dummy_post( $cur_day, 'draft' );

// 1 for each day in last week for CPT
for ( $i = 0; $i < 6; $i++ ) {
$date = strtotime( '-1 day', $date );
$cur_day = date( 'Y', $date ) . '-' . date( 'm', $date ) . '-' . date( 'd', $date ) . ' 00:00:00';
$this->test_base->create_dummy_post( $cur_day, 'publish', self::TEST_CPT );
$this->create_dummy_post( $cur_day, 'publish', self::TEST_CPT );
}

// 1 CPT draft for a month ago
$date = strtotime( '-1 month', time() );
$date = strtotime( '-1 month' );
$cur_day = date( 'Y', $date ) . '-' . date( 'm', $date ) . '-' . date( 'd', $date ) . ' 00:00:00';
$this->test_base->create_dummy_post( $cur_day, 'draft', self::TEST_CPT );
$this->create_dummy_post( $cur_day, 'draft', self::TEST_CPT );

$this->assertCount( 10, $this->test_base->posts );
$this->test_base->build_sitemaps();
$this->assertCount( 10, $this->posts );
$this->build_sitemaps();

}

/**
* Remove the sample posts and the sitemap posts
*/
function teardown(): void {
$this->test_base->posts = array();
public function teardown(): void {
$this->posts = array();
$sitemaps = get_posts( array(
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'fields' => 'ids',
'posts_per_page' => -1,
) );
update_option( 'msm_sitemap_indexed_url_count' , 0 );
array_map( 'wp_delete_post', array_merge( $this->test_base->posts_created, $sitemaps ) );
array_map( 'wp_delete_post', array_merge( $this->posts_created, $sitemaps ) );
}

/**
* Verify Custom Post Types don't have Sitemaps generated by default
*/
function test_cpt_ignored_by_default() {
public function test_cpt_ignored_by_default(): void
{

$this->test_base->build_sitemaps();
$this->build_sitemaps();
$sitemaps = get_posts( array(
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'fields' => 'ids',
Expand All @@ -119,11 +112,12 @@ function test_cpt_ignored_by_default() {
* Verify Custom Post Types included when "msm_sitemap_entry_post_type"
* filter applied
*/
function test_cpt_included_by_filter() {
public function test_cpt_included_by_filter(): void
{

add_filter( 'msm_sitemap_entry_post_type', array( $this, 'add_cpt_to_msm_sitemap' ) );

$this->test_base->build_sitemaps();
$this->build_sitemaps();
$sitemaps = get_posts( array(
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'fields' => 'ids',
Expand All @@ -138,9 +132,11 @@ function test_cpt_included_by_filter() {
* Filter hook to add TEST_CPT to Metro_Sitemap supported post types
*
* @param array $cpts Array of Post Types.
* @return array(str) Array of Post Types.
*
* @return array<string> Array of Post Types.
*/
public function add_cpt_to_msm_sitemap( $cpts ) {
public function add_cpt_to_msm_sitemap( array $cpts ): array
{
$cpts[] = self::TEST_CPT;
return $cpts;
}
Expand Down
Loading

0 comments on commit c494c3a

Please sign in to comment.