Skip to content

Commit fd0099b

Browse files
obenlandpfefferle
andauthored
Tests: Make it easy to get code coverage reports (#1386)
* Tests: Make it easy to get code coverage reports * Fix coverage annotations * ignore coverage folder * Add coverage for integrations --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent ffabbfa commit fd0099b

14 files changed

+117
-81
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ _site
22
.sass-cache
33
.jekyll-cache
44
.jekyll-metadata
5+
/coverage/
56
/node_modules/
67
/vendor/
78
package-lock.json

.wp-env.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"port": 80,
1212
"mappings": {
1313
"wp-content/plugins/activitypub": ".",
14-
"wp-content/plugins/activitypub/tests": "./tests"
14+
"wp-content/plugins/activitypub/tests": "./tests",
15+
"wp-content/plugins/activitypub/coverage": "./coverage"
1516
}
1617
}
1718
}

docs/development-environment.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,29 @@ Common PHPUnit arguments:
9999
- `--exclude-group`: Exclude tests with a specific @group annotation
100100
- `--verbose`: Output more verbose information
101101
- `--debug`: Display debugging information
102+
103+
### Code Coverage Reports
104+
105+
The coverage configuration is already set up in `phpunit.xml.dist` to analyze the code in the `includes` directory. To generate code coverage reports, you'll need to start wp-env with Xdebug enabled for coverage:
106+
107+
```bash
108+
# Start the environment with Xdebug enabled.
109+
npm run env -- start --xdebug=coverage
110+
```
111+
```bash
112+
# Run tests with code coverage.
113+
npm run env-test -- --coverage-text
114+
```
115+
116+
The above will display a text-based coverage report in your terminal. For a more detailed HTML report:
117+
118+
```bash
119+
# Generate HTML coverage report in Docker.
120+
npm run env-test -- --coverage-html ./coverage
121+
```
122+
```bash
123+
# Open the coverage report in your default browser (macOS).
124+
open coverage/index.html
125+
```
126+
127+
The HTML report will be generated directly in the `coverage` directory in your local filesystem. The `index.html` file can then be opened in a browser, showing a detailed analysis of which lines of code are covered by tests.

includes/activity/class-base-object.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @method string|null get_actor() Gets one or more entities that performed or are expected to perform the activity.
2525
* @method string|null get_attributed_to() Gets the entity attributed as the original author.
2626
* @method array|null get_attachment() Gets the attachment property of the object.
27-
* @method string|null get_cc() Gets the secondary recipients of the object.
27+
* @method array|null get_cc() Gets the secondary recipients of the object.
2828
* @method string|null get_content() Gets the content property of the object.
2929
* @method array|null get_icon() Gets the icon property of the object.
3030
* @method string|null get_id() Gets the object's unique global identifier.

phpunit.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@
1414
<listeners>
1515
<listener class="Activitypub\Tests\Activitypub_Testcase_Timer" file="tests/class-activitypub-testcase-timer.php" />
1616
</listeners>
17+
<coverage>
18+
<include>
19+
<directory suffix=".php">./includes</directory>
20+
<directory suffix=".php">./integration</directory>
21+
</include>
22+
</coverage>
1723
</phpunit>

tests/includes/activity/class-test-base-object.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function test_to_string() {
3131
/**
3232
* Test the magic add method.
3333
*
34-
* @covers ::add_* Magic function.
34+
* @covers ::__call
3535
*
3636
* @dataProvider data_magic_add
3737
*

tests/includes/class-test-comment.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Test_Comment extends \WP_UnitTestCase {
1919
/**
2020
* Test get source id or url.
2121
*
22-
* @covers ::get_source_id, ::get_source_url
22+
* @covers ::get_source_id
23+
* @covers ::get_source_url
2324
*/
2425
public function test_get_source_id_or_url() {
2526
$comment_id = wp_insert_comment(
@@ -115,47 +116,6 @@ public function test_check_ability_to_federate_threaded_comment( $parent_comment
115116
$this->assertEquals( $expected['should_be_federated'], Comment::should_be_federated( $comment ) );
116117
}
117118

118-
/**
119-
* Test get comment ancestors.
120-
*
121-
* @covers ::get_comment_ancestors
122-
*/
123-
public function test_get_comment_ancestors() {
124-
$comment_id = wp_insert_comment(
125-
array(
126-
'comment_type' => 'comment',
127-
'comment_content' => 'This is a comment.',
128-
'comment_author_url' => 'https://example.com',
129-
'comment_author_email' => '',
130-
'comment_meta' => array(
131-
'protocol' => 'activitypub',
132-
),
133-
)
134-
);
135-
136-
$this->assertEquals( array(), \Activitypub\get_comment_ancestors( $comment_id ) );
137-
138-
$comment_array = get_comment( $comment_id, ARRAY_A );
139-
140-
$parent_comment_id = wp_insert_comment(
141-
array(
142-
'comment_type' => 'parent comment',
143-
'comment_content' => 'This is a parent comment.',
144-
'comment_author_url' => 'https://example.com',
145-
'comment_author_email' => '',
146-
'comment_meta' => array(
147-
'protocol' => 'activitypub',
148-
),
149-
)
150-
);
151-
152-
$comment_array['comment_parent'] = $parent_comment_id;
153-
154-
wp_update_comment( $comment_array );
155-
156-
$this->assertEquals( array( $parent_comment_id ), \Activitypub\get_comment_ancestors( $comment_id ) );
157-
}
158-
159119
/**
160120
* Test pre_comment_approved.
161121
*

tests/includes/class-test-functions.php

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function set_up() {
3636
/**
3737
* Test the get_remote_metadata_by_actor function.
3838
*
39-
* @covers ::get_remote_metadata_by_actor
39+
* @covers \ActivityPub\get_remote_metadata_by_actor
4040
*/
4141
public function test_get_remote_metadata_by_actor() {
4242
$metadata = \ActivityPub\get_remote_metadata_by_actor( '[email protected]' );
@@ -48,7 +48,7 @@ public function test_get_remote_metadata_by_actor() {
4848
/**
4949
* Test object_id_to_comment.
5050
*
51-
* @covers ::object_id_to_comment
51+
* @covers \ActivityPub\object_id_to_comment
5252
*/
5353
public function test_object_id_to_comment_basic() {
5454
$single_comment_source_id = 'https://example.com/single';
@@ -80,7 +80,7 @@ public function test_object_id_to_comment_basic() {
8080
/**
8181
* Test object_id_to_comment with invalid source ID.
8282
*
83-
* @covers ::object_id_to_comment
83+
* @covers \ActivityPub\object_id_to_comment
8484
*/
8585
public function test_object_id_to_comment_none() {
8686
$single_comment_source_id = 'https://example.com/none';
@@ -91,7 +91,7 @@ public function test_object_id_to_comment_none() {
9191
/**
9292
* Test object_id_to_comment with duplicate source ID.
9393
*
94-
* @covers ::object_id_to_comment
94+
* @covers \ActivityPub\object_id_to_comment
9595
*/
9696
public function test_object_id_to_comment_duplicate() {
9797
$duplicate_comment_source_id = 'https://example.com/duplicate';
@@ -129,7 +129,7 @@ public function test_object_id_to_comment_duplicate() {
129129
* Test object_to_uri.
130130
*
131131
* @dataProvider object_to_uri_provider
132-
* @covers ::object_to_uri
132+
* @covers \Activitypub\object_to_uri
133133
*
134134
* @param mixed $input The input to test.
135135
* @param mixed $output The expected output.
@@ -141,7 +141,7 @@ public function test_object_to_uri( $input, $output ) {
141141
/**
142142
* Test is_self_ping.
143143
*
144-
* @covers ::is_self_ping
144+
* @covers \Activitypub\is_self_ping
145145
*/
146146
public function test_is_self_ping() {
147147
$this->assertFalse( \Activitypub\is_self_ping( 'https://example.org' ) );
@@ -220,7 +220,7 @@ public function object_to_uri_provider() {
220220
/**
221221
* Test is_activity with array input.
222222
*
223-
* @covers ::is_activity
223+
* @covers \Activitypub\is_activity
224224
*
225225
* @dataProvider is_activity_data
226226
*
@@ -280,7 +280,7 @@ public function is_activity_data() {
280280
/**
281281
* Test is_activity with invalid input.
282282
*
283-
* @covers ::is_activity
283+
* @covers \Activitypub\is_activity
284284
*/
285285
public function test_is_activity_with_invalid_input() {
286286
$invalid_inputs = array(
@@ -300,10 +300,51 @@ public function test_is_activity_with_invalid_input() {
300300
}
301301
}
302302

303+
/**
304+
* Test get comment ancestors.
305+
*
306+
* @covers \Activitypub\get_comment_ancestors
307+
*/
308+
public function test_get_comment_ancestors() {
309+
$comment_id = wp_insert_comment(
310+
array(
311+
'comment_type' => 'comment',
312+
'comment_content' => 'This is a comment.',
313+
'comment_author_url' => 'https://example.com',
314+
'comment_author_email' => '',
315+
'comment_meta' => array(
316+
'protocol' => 'activitypub',
317+
),
318+
)
319+
);
320+
321+
$this->assertEquals( array(), \Activitypub\get_comment_ancestors( $comment_id ) );
322+
323+
$comment_array = get_comment( $comment_id, ARRAY_A );
324+
325+
$parent_comment_id = wp_insert_comment(
326+
array(
327+
'comment_type' => 'parent comment',
328+
'comment_content' => 'This is a parent comment.',
329+
'comment_author_url' => 'https://example.com',
330+
'comment_author_email' => '',
331+
'comment_meta' => array(
332+
'protocol' => 'activitypub',
333+
),
334+
)
335+
);
336+
337+
$comment_array['comment_parent'] = $parent_comment_id;
338+
339+
wp_update_comment( $comment_array );
340+
341+
$this->assertEquals( array( $parent_comment_id ), \Activitypub\get_comment_ancestors( $comment_id ) );
342+
}
343+
303344
/**
304345
* Test is_post_disabled function.
305346
*
306-
* @covers ::is_post_disabled
347+
* @covers \Activitypub\is_post_disabled
307348
*/
308349
public function test_is_post_disabled() {
309350
// Test standard public post.
@@ -356,7 +397,7 @@ public function test_is_post_disabled() {
356397
/**
357398
* Test is_post_disabled with private visibility.
358399
*
359-
* @covers ::is_post_disabled
400+
* @covers \Activitypub\is_post_disabled
360401
*/
361402
public function test_is_post_disabled_private_visibility() {
362403
$visible_private_post_id = self::factory()->post->create();
@@ -377,7 +418,7 @@ public function test_is_post_disabled_private_visibility() {
377418
/**
378419
* Test is_post_disabled with invalid post.
379420
*
380-
* @covers ::is_post_disabled
421+
* @covers \Activitypub\is_post_disabled
381422
*/
382423
public function test_is_post_disabled_invalid_post() {
383424
$this->assertTrue( \Activitypub\is_post_disabled( 0 ) );
@@ -388,7 +429,7 @@ public function test_is_post_disabled_invalid_post() {
388429
/**
389430
* Test get_masked_wp_version function.
390431
*
391-
* @covers ::get_masked_wp_version
432+
* @covers \Activitypub\get_masked_wp_version
392433
* @dataProvider provide_wp_versions
393434
*
394435
* @param string $input The input version.

tests/includes/class-test-migration.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ public function test_migrate_actor_mode() {
178178
$this->assertEquals( ACTIVITYPUB_ACTOR_MODE, \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) );
179179
}
180180

181+
/**
182+
* Tests scheduling of migration.
183+
*
184+
* @covers ::maybe_migrate
185+
*/
186+
public function test_migration_scheduling() {
187+
update_option( 'activitypub_db_version', '0.0.1' );
188+
189+
Migration::maybe_migrate();
190+
191+
$schedule = \wp_next_scheduled( 'activitypub_migrate', array( '0.0.1' ) );
192+
$this->assertNotFalse( $schedule );
193+
194+
// Clean up.
195+
delete_option( 'activitypub_db_version' );
196+
}
197+
181198
/**
182199
* Test migrate to 4.1.0.
183200
*

tests/includes/collection/class-test-extra-fields.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Test_Extra_Fields extends \WP_UnitTestCase {
1919
/**
2020
* Test the get_attachment.
2121
*
22-
* @covers ::get_attachment
22+
* @covers ::fields_to_attachments
2323
*/
2424
public function test_get_attachment() {
2525
$post = self::factory()->post->create_and_get(

0 commit comments

Comments
 (0)