Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Tests

on:
push:
branches: [ main ]
pull_request:

jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: [ '7.4', '8.1', '8.3' ]
steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: pcov

- run: composer update --prefer-dist --no-progress

- run: vendor/bin/phpunit --coverage-text
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ wp-config.php
#
# Note: If you wish to whitelist themes,
# uncomment the next line
#/wp-content/themes
#/wp-content/themes

# Composer / PHPUnit
/vendor/
/composer.lock
.phpunit.result.cache
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ Paigaldus
2. Aktiveeri WordPressi adminis (Plugins → DoneProject Plugin).
3. Lisa lehele/ postitusele shortcode (näide eespool).

Testid

Unit-testid kasutavad PHPUnitit ja WordPressi funktsioonide asendajaid (`tests/wp-stubs.php`), seega WordPressi installatsiooni pole vaja.

1. `composer install`
2. `composer test` (või `vendor/bin/phpunit`)
3. Kaetuse aruanne: `php -d pcov.enabled=1 vendor/bin/phpunit --coverage-text` (nõuab pcov või Xdebug laiendust)

Piirangud ja tähelepanekud

- Pages ja category: vaikimisi ei kasuta WordPress pages taksonoomiat "category"; kui kasutad post_type="page" ja sinu lehed ei ole category-ga seotud, shortcode ei tagasta tulemusi — kasuta post tüüpi või registreeri category ka lehtedele.
Expand Down
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
},
"autoload": {
"psr-4": {
"DoneProject\\\\": "src/"
"DoneProject\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"DoneProject\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit",
"test:coverage": "phpunit --coverage-text"
},
"require-dev": {
"phpunit/phpunit": "^9.6"
}
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
beStrictAboutTestsThatDoNotTestAnything="false"
convertDeprecationsToExceptions="true">
<testsuites>
<testsuite name="doneproject">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
106 changes: 106 additions & 0 deletions tests/AdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
namespace DoneProject\Tests;

use DoneProject\Admin;

class AdminTest extends TestCase {
public function testConstructorHooksIntoAdminInitAndMenu(): void {
$admin = new Admin( $this->pluginStub() );

$hooks = array();
foreach ( WPStub::callsTo( 'add_action' ) as $call ) {
$hooks[ $call[0] ] = $call[1];
}

$this->assertSame( array( $admin, 'register_settings' ), $hooks['admin_init'] );
$this->assertSame( array( $admin, 'add_menu' ), $hooks['admin_menu'] );
}

public function testAddMenuRegistersOptionsPage(): void {
$admin = new Admin( $this->pluginStub() );

$admin->add_menu();

$this->assertSame(
array( 'DoneProject Settings', 'DoneProject', 'manage_options', 'doneproject-plugin', array( $admin, 'settings_page' ) ),
WPStub::firstCall( 'add_options_page' )
);
}

public function testRegisterSettingsRegistersOptionSectionAndField(): void {
$admin = new Admin( $this->pluginStub() );

$admin->register_settings();

$setting = WPStub::firstCall( 'register_setting' );
$this->assertSame( 'doneproject-plugin_group', $setting[0] );
$this->assertSame( 'doneproject-plugin_greeting', $setting[1] );
$this->assertSame(
array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Tere' ),
$setting[2]
);

$section = WPStub::firstCall( 'add_settings_section' );
$this->assertSame( 'doneproject-plugin_section', $section[0] );
$this->assertSame( 'doneproject-plugin', $section[3] );

$field = WPStub::firstCall( 'add_settings_field' );
$this->assertSame( 'doneproject-plugin_greeting_field', $field[0] );
$this->assertSame( array( $admin, 'field_html' ), $field[2] );
$this->assertSame( 'doneproject-plugin', $field[3] );
$this->assertSame( 'doneproject-plugin_section', $field[4] );
}

public function testFieldHtmlRendersStoredValue(): void {
WPStub::$options['doneproject-plugin_greeting'] = 'Hei';
$admin = new Admin( $this->pluginStub() );

ob_start();
$admin->field_html();
$html = ob_get_clean();

$this->assertSame(
'<input type="text" name="doneproject-plugin_greeting" value="Hei" class="regular-text" />',
$html
);
}

public function testFieldHtmlRendersDefaultAndEscapesValue(): void {
WPStub::$options['doneproject-plugin_greeting'] = 'Tere "sõber" & <b>';
$admin = new Admin( $this->pluginStub() );

ob_start();
$admin->field_html();
$html = ob_get_clean();

$this->assertStringContainsString( 'value="Tere &quot;s', $html );
$this->assertStringNotContainsString( '<b>', $html );
}

public function testSettingsPageRendersFormForPermittedUser(): void {
$admin = new Admin( $this->pluginStub() );

ob_start();
$admin->settings_page();
$html = ob_get_clean();

$this->assertStringContainsString( 'DoneProject seaded', $html );
$this->assertStringContainsString( '<form method="post" action="options.php">', $html );
$this->assertSame( array( 'doneproject-plugin_group' ), WPStub::firstCall( 'settings_fields' ) );
$this->assertSame( array( 'doneproject-plugin' ), WPStub::firstCall( 'do_settings_sections' ) );
$this->assertTrue( WPStub::called( 'submit_button' ) );
}

public function testSettingsPageRendersNothingWithoutCapability(): void {
WPStub::$current_user_can = false;
$admin = new Admin( $this->pluginStub() );

ob_start();
$admin->settings_page();
$html = ob_get_clean();

$this->assertSame( '', $html );
$this->assertFalse( WPStub::called( 'settings_fields' ) );
$this->assertSame( array( 'manage_options' ), WPStub::firstCall( 'current_user_can' ) );
}
}
Loading
Loading