Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Tests\Unit\AI_Free_Sparks\User_Interface\Free_Sparks_Route;

use Brain\Monkey\Functions;
use Mockery;
use WP_User;

/**
* Tests the Free_Sparks_Route's can_edit_posts method.
*
* @group ai-free-sparks
*
* @covers \Yoast\WP\SEO\AI_Free_Sparks\User_Interface\Free_Sparks_Route::can_edit_posts
*/
final class Can_Edit_Posts_Test extends Abstract_Free_Sparks_Route_Test {

/**
* Tests can_edit_posts returns false if no user is logged in.
*
* @return void
*/
public function test_can_edit_posts_returns_false_when_no_user() {
Functions\expect( 'wp_get_current_user' )
->once()
->andReturn( null );

$this->assertFalse( $this->instance->can_edit_posts() );
}

/**
* Tests can_edit_posts returns false if user ID < 1.
*
* @return void
*/
public function test_can_edit_posts_returns_false_when_user_id_less_than_1() {
$user = Mockery::mock( WP_User::class );
$user->ID = 0;
Functions\expect( 'wp_get_current_user' )
->once()
->andReturn( $user );

$this->assertFalse( $this->instance->can_edit_posts() );
}

/**
* Tests can_edit_posts returns true if user can edit posts.
*
* @return void
*/
public function test_can_edit_posts_returns_true_when_user_can_edit_posts() {
$user = Mockery::mock( WP_User::class );
$user->ID = 5;
Functions\expect( 'wp_get_current_user' )
->once()
->andReturn( $user );
Functions\expect( 'user_can' )
->once()
->with( $user, 'edit_posts' )
->andReturn( true );

$this->assertTrue( $this->instance->can_edit_posts() );
}

/**
* Tests can_edit_posts returns false if user cannot edit posts.
*
* @return void
*/
public function test_can_edit_posts_returns_false_when_user_cannot_edit_posts() {
$user = Mockery::mock( WP_User::class );
$user->ID = 5;
Functions\expect( 'wp_get_current_user' )
->once()
->andReturn( $user );
Functions\expect( 'user_can' )
->once()
->with( $user, 'edit_posts' )
->andReturn( false );

$this->assertFalse( $this->instance->can_edit_posts() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Tests\Unit\AI_Free_Sparks\User_Interface\Free_Sparks_Route;

use Yoast\WP\SEO\AI_Free_Sparks\User_Interface\Free_Sparks_Route;
use Yoast\WP\SEO\Conditionals\AI_Conditional;

/**
* Tests the Free_Sparks_Route's conditional.
*
* @group ai-free-sparks
*
* @covers \Yoast\WP\SEO\AI_Free_Sparks\User_Interface\Free_Sparks_Route::get_conditionals
*/
final class Conditional_Test extends Abstract_Free_Sparks_Route_Test {

/**
* Tests the conditional.
*
* @return void
*/
public function test_conditional() {
$expected = [ AI_Conditional::class ];
$this->assertSame( $expected, Free_Sparks_Route::get_conditionals() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Tests\Unit\AI_Free_Sparks\User_Interface\Free_Sparks_Route;

use Yoast\WP\SEO\AI_Free_Sparks\Application\Free_Sparks_Handler_Interface;

/**
* Tests the Free_Sparks_Route's construct method.
*
* @group ai-free-sparks
*
* @covers \Yoast\WP\SEO\AI_Free_Sparks\User_Interface\Free_Sparks_Route::__construct
*/
final class Constructor_Test extends Abstract_Free_Sparks_Route_Test {

/**
* Tests the constructor.
*
* @return void
*/
public function test_constructor() {
$this->assertInstanceOf(
Free_Sparks_Handler_Interface::class,
$this->getPropertyValue( $this->instance, 'free_sparks_handler' )
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Tests\Unit\AI_Free_Sparks\User_Interface\Free_Sparks_Route;

use Brain\Monkey\Functions;

/**
* Tests the Free_Sparks_Route's register_routes method.
*
* @group ai-free-sparks
*
* @covers \Yoast\WP\SEO\AI_Free_Sparks\User_Interface\Free_Sparks_Route::register_routes
*/
final class Register_Routes_Test extends Abstract_Free_Sparks_Route_Test {

/**
* Tests that register_routes registers the expected route.
*
* @return void
*/
public function test_register_routes() {
Functions\expect( 'register_rest_route' )
->once()
->with(
'yoast/v1',
'/ai/free_sparks',
[
'methods' => 'POST',
'callback' => [ $this->instance, 'start' ],
'permission_callback' => [ $this->instance, 'can_edit_posts' ],
]
);

$this->instance->register_routes();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Tests\Unit\AI_Free_Sparks\User_Interface\Free_Sparks_Route;

use Mockery;
use WP_REST_Response;

/**
* Tests the Free_Sparks_Route's start method.
*
* @group ai-free-sparks
*
* @covers \Yoast\WP\SEO\AI_Free_Sparks\User_Interface\Free_Sparks_Route::start
*/
final class Start_Test extends Abstract_Free_Sparks_Route_Test {

/**
* Tests start method when successful.
*
* @return void
*/
public function test_start_success() {
$this->free_sparks_handler->expects( 'start' )
->once()
->with( null )
->andReturn( true );

mockery::mock( 'overload:' . WP_REST_Response::class );

$result = $this->instance->start();
$this->assertInstanceOf( WP_REST_Response::class, $result );
$this->assertEquals( new WP_REST_Response( 'Free sparks successfully started.' ), $result );
}

/**
* Tests start when fails.
*
* @return void
*/
public function test_start_fail() {
$this->free_sparks_handler->expects( 'start' )
->once()
->with( null )
->andReturn( false );

mockery::mock( 'overload:' . WP_REST_Response::class );

$result = $this->instance->start();
$this->assertInstanceOf( WP_REST_Response::class, $result );
$this->assertEquals( new WP_REST_Response( 'Failed to start free sparks.', 500 ), $result );
}
}