Skip to content
Draft
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
3 changes: 2 additions & 1 deletion src/PostData.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public function children(array $args = []): Collection
$args = wp_parse_args($args, [
'post_parent' => $this->id,
'post_type' => $this->postType,
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'menu_order',
]);
Expand All @@ -308,7 +309,7 @@ public function parent(): ?static
return null;
}
$parent = get_post_parent($this->id);
if (null === $parent) {
if (null === $parent || 'publish' !== $parent->post_status) {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function tearDown(): void
protected function getPackageProviders($app)
{
return [
'Spatie\LaravelData\LaravelDataServiceProvider',
'Yard\Data\Providers\DataServiceProvider',
];
}
Expand Down
65 changes: 65 additions & 0 deletions tests/src/PostDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,68 @@

expect($this->postData->url())->toBe('');
});

it('only queries published children', function () {
\WP_Mock::userFunction('is_post_type_hierarchical', [
'args' => ['post'],
'return' => true,
]);

\WP_Mock::userFunction('wp_parse_args', [
'return' => fn (array $args, array $defaults): array => array_merge($defaults, $args),
]);

$queried = [];

\WP_Mock::userFunction('get_children', [
'return' => function (array $args) use (&$queried): array {
$queried = $args;

return [];
},
]);

$this->postData->children();

expect($queried['post_status'])->toBe('publish');
});

it('lets callers override the child post status', function () {
\WP_Mock::userFunction('is_post_type_hierarchical', [
'args' => ['post'],
'return' => true,
]);

\WP_Mock::userFunction('wp_parse_args', [
'return' => fn (array $args, array $defaults): array => array_merge($defaults, $args),
]);

$queried = [];

\WP_Mock::userFunction('get_children', [
'return' => function (array $args) use (&$queried): array {
$queried = $args;

return [];
},
]);

$this->postData->children(['post_status' => 'any']);

expect($queried['post_status'])->toBe('any');
});

it('has no parent when the parent is not published', function () {
\WP_Mock::userFunction('is_post_type_hierarchical', [
'args' => ['post'],
'return' => true,
]);

\WP_Mock::userFunction('get_post_parent', [
'args' => [1],
'return' => (object) ['ID' => 2, 'post_status' => 'draft'],
]);

expect($this->postData->parent())->toBeNull();
expect($this->postData->isChild())->toBeFalse();
});
Loading