Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more PHP Unit Tests #101

Merged
merged 10 commits into from
Mar 21, 2022
260 changes: 218 additions & 42 deletions tests/phpunit/SimpleLocalAvatarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,65 @@ public function setUp(): void {
$this->instance = Mockery::mock( 'Simple_Local_Avatars' )->makePartial();

// Set class properties
$reflection = new ReflectionClass( 'Simple_Local_Avatars' );
$user_property = $reflection->getProperty( 'user_key' );
$rating_property = $reflection->getProperty( 'rating_key' );
$reflection = new ReflectionClass( 'Simple_Local_Avatars' );

$user_property = $reflection->getProperty( 'user_key' );
$user_property->setAccessible( true );
$user_property->setValue( $this->instance, 'simple_local_avatar' );

$rating_property = $reflection->getProperty( 'rating_key' );
$rating_property->setAccessible( true );
$rating_property->setValue( $this->instance, 'simple_local_avatar_rating' );

$avatar_ratings = $reflection->getProperty( 'avatar_ratings' );
$avatar_ratings->setAccessible( true );
$avatar_ratings->setValue( $this->instance, array(
'G' => __( 'G — Suitable for all audiences', 'simple-local-avatars' ),
'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above', 'simple-local-avatars' ),
'R' => __( 'R — Intended for adult audiences above 17', 'simple-local-avatars' ),
'X' => __( 'X — Even more mature than above', 'simple-local-avatars' ),
) );

$user = (object) [
'ID' => 1
'ID' => 1,
'display_name' => 'TEST_USER',
];

// Init $POST.
$_POST = array();

WP_Mock::userFunction( 'get_user_by' )
->with( 'id', 0 )
->andReturn( $user );

WP_Mock::userFunction( 'get_user_by' )
->with( 'email', '' )
->andReturn( false );
->with( 'email', '' )
->andReturn( false );

WP_Mock::userFunction( 'get_user_by' )
->with( 'email', Mockery::type( 'string' ) )
->andReturn( $user );
->with( 'email', Mockery::type( 'string' ) )
->andReturn( $user );

WP_Mock::userFunction( 'get_user_meta' )
->with( 1, 'simple_local_avatar', true )
->andReturn( [
'media_id' => 101,
'full' => 'https://example.com/avatar.png',
'96' => 'https://example.com/avatar-96x96.png',
] )
->byDefault();
->with( 1, 'simple_local_avatar', true )
->andReturn( [
'media_id' => 101,
'full' => 'https://example.com/avatar.png',
'96' => 'https://example.com/avatar-96x96.png',
] )
->byDefault();

WP_Mock::userFunction( 'get_user_meta' )
->with( Mockery::type( 'numeric' ), 'simple_local_avatar_rating', true )
->andReturn( 'G' );
->with( Mockery::type( 'numeric' ), 'simple_local_avatar_rating', true )
->andReturn( 'G' );

WP_Mock::userFunction( 'get_attached_file' )
->with( 101 )
->andReturn( '/avatar.png' );
->with( 101 )
->andReturn( '/avatar.png' );

WP_Mock::userFunction( 'admin_url' )
->with( 'options-discussion.php' )
->andReturn( 'wp-admin/options-discussion.php' );
->with( 'options-discussion.php' )
->andReturn( 'wp-admin/options-discussion.php' );

}

Expand All @@ -61,9 +79,9 @@ public function tearDown(): void {
}

public function test_add_hooks() {
WP_Mock::expectFilterAdded( 'plugin_action_links_' . SLA_PLUGIN_BASENAME, [ $this->instance, 'plugin_filter_action_links'] );
WP_Mock::expectFilterAdded( 'plugin_action_links_' . SLA_PLUGIN_BASENAME, [ $this->instance, 'plugin_filter_action_links' ] );

WP_Mock::expectFilterAdded( 'pre_get_avatar_data', [ $this->instance, 'get_avatar_data'], 10, 2 );
WP_Mock::expectFilterAdded( 'pre_get_avatar_data', [ $this->instance, 'get_avatar_data' ], 10, 2 );
WP_Mock::expectFilterAdded( 'pre_option_simple_local_avatars', [ $this->instance, 'pre_option_simple_local_avatars' ], 10, 1 );

WP_Mock::expectActionAdded( 'admin_init', [ $this->instance, 'admin_init' ] );
Expand Down Expand Up @@ -123,14 +141,14 @@ public function test_is_avatar_shared() {
public function test_get_avatar() {
$img = '<img src="https://example.com/avatar.png" />';
$filtered_img = '<img src="https://example.com/avatar-filtered.png" />';
WP_Mock::userFunction( 'get_avatar')->andReturn( $img );
WP_Mock::userFunction( 'get_avatar' )->andReturn( $img );
WP_Mock::expectFilter( 'simple_local_avatar', $img );

$this->assertEquals( $img, $this->instance->get_avatar() );

WP_Mock::onFilter( 'simple_local_avatar' )
->with( $img )
->reply( $filtered_img );
->with( $img )
->reply( $filtered_img );

$this->assertEquals( $filtered_img, $this->instance->get_avatar() );
}
Expand All @@ -149,8 +167,8 @@ public function test_plugin_filter_action_links() {
}

public function test_get_avatar_data() {
$avatar_data = $this->instance->get_avatar_data( [ 'size' => 96 ], 1 );
$this->assertEquals( 'https://example.com/avatar-96x96.png', $avatar_data['url'] );
$avatar_data = $this->instance->get_avatar_data( [ 'size' => 96 ], 1 );
$this->assertEquals( 'https://example.com/avatar-96x96.png', $avatar_data['url'] );
}

public function test_get_simple_local_avatar_url_with_empty_id() {
Expand All @@ -159,18 +177,18 @@ public function test_get_simple_local_avatar_url_with_empty_id() {

public function test_get_simple_local_avatar_url_user_with_no_avatar() {
WP_Mock::userFunction( 'get_user_meta' )
->with( 2, 'simple_local_avatar', true )
->andReturn( [] );
->with( 2, 'simple_local_avatar', true )
->andReturn( [] );
$this->assertEquals( '', $this->instance->get_simple_local_avatar_url( 2, 96 ) );
}

public function test_get_simple_local_avatar_url_media_file_deleted() {
WP_Mock::userFunction( 'get_user_meta' )
->with( 2, 'simple_local_avatar', true )
->andReturn( ['media_id' => 102 ] );
->with( 2, 'simple_local_avatar', true )
->andReturn( [ 'media_id' => 102 ] );
WP_Mock::userFunction( 'get_attached_file' )
->with( 102 )
->andReturn( false );
->with( 102 )
->andReturn( false );
$this->assertEquals( '', $this->instance->get_simple_local_avatar_url( 2, 96 ) );
}

Expand All @@ -180,8 +198,8 @@ public function test_get_simple_local_avatar_url() {

public function test_admin_init() {
WP_Mock::userFunction( 'get_option' )
->with( 'simple_local_avatars_caps' )
->andReturn( false );
->with( 'simple_local_avatars_caps' )
->andReturn( false );

WP_Mock::userFunction( 'register_setting' );
WP_Mock::userFunction( 'add_settings_field' );
Expand All @@ -203,7 +221,7 @@ public function test_admin_enqueue_scripts_wrong_screen() {
}

public function test_sanitize_options() {
$input = [
$input = [
'caps' => true,
];
$new_input = $this->instance->sanitize_options( $input );
Expand All @@ -213,6 +231,164 @@ public function test_sanitize_options() {
$this->assertSame( 0, $new_input['only'] );
}

public function test_get_default_avatar_url() {
WP_Mock::userFunction( 'get_option' )
->with( 'avatar_default' )
->andReturn( '' );

WP_Mock::userFunction( 'is_ssl' )
->andReturn( true );

$size = 90;
$returned_url = $this->instance->get_default_avatar_url( $size );
$expected_url = 'https://secure.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=90';
$this->assertEquals( $expected_url, $returned_url );
}

public function test_avatar_settings_field() {
$args = array(
'key' => 'shared',
'desc' => 'This is a description.',
'default' => 0,
);

WP_Mock::userFunction( 'wp_parse_args' )
->with( $args,
array(
'key' => '',
'desc' => '',
'default' => 0,
) )
->andReturn( $args );

WP_Mock::userFunction( 'checked' )
->andReturn( 'checked' );

$expected = '<label for="simple-local-avatars-shared"><input type="checkbox" name="simple_local_avatars[shared]" id="simple-local-avatars-shared" value="1" checked />This is a description.</label>';

$this->expectOutputString( $expected );

$this->instance->avatar_settings_field( $args );
}

public function test_edit_user_profile() {

WP_Mock::userFunction( 'remove_filter' );
WP_Mock::userFunction( 'wp_nonce_field' );
WP_Mock::userFunction( 'add_query_arg' );
WP_Mock::userFunction( 'disabled' );

WP_Mock::userFunction( 'get_simple_local_avatar' )
->with( 1 )
->andReturn( '<img src="test-image-user-avatar"/>' );

$profileuser = new stdClass();
$profileuser->ID = 1;

$expected = '<div id="simple-local-avatar-section"><h3>Avatar</h3><table class="form-table"><tr class="upload-avatar-row"><th scope="row"><label for="simple-local-avatar">Upload Avatar</label></th><td style="width: 50px;" id="simple-local-avatar-photo"><img src="test-image-user-avatar"/></td><td><p style="display: inline-block; width: 26em;"><span class="description">Choose an image from your computer:</span><br /><input type="file" name="simple-local-avatar" id="simple-local-avatar" class="standard-text" /><span class="spinner" id="simple-local-avatar-spinner"></span></p><p><a href="" class="button item-delete submitdelete deletion" id="simple-local-avatar-remove" style="display:none;">Delete local avatar</a></p></td></tr><tr class="ratings-row"><th scope="row">Rating</th><td colspan="2"><fieldset id="simple-local-avatar-ratings" ><legend class="screen-reader-text"><span>Rating</span></legend><label><input type=\'radio\' name=\'simple_local_avatar_rating\' value=\'G\' />G &#8212; Suitable for all audiences</label><br /><label><input type=\'radio\' name=\'simple_local_avatar_rating\' value=\'PG\' />PG &#8212; Possibly offensive, usually for audiences 13 and above</label><br /><label><input type=\'radio\' name=\'simple_local_avatar_rating\' value=\'R\' />R &#8212; Intended for adult audiences above 17</label><br /><label><input type=\'radio\' name=\'simple_local_avatar_rating\' value=\'X\' />X &#8212; Even more mature than above</label><br /><p class="description">If the local avatar is inappropriate for this site, Gravatar will be attempted.</p></fieldset></td></tr></table></div>';

$this->expectOutputString( $expected );

$this->instance->edit_user_profile( $profileuser );
}

public function test_assign_new_user_avatar() {
$user_id = 1;
$url_or_media_id = 0;

$meta_value = array(
'media_id' => 0,
'full' => '/full_url',
'blog_id' => 101,
);

WP_Mock::userFunction( 'wp_upload_dir' )
->andReturn( array(
'baseurl' => '/example',
'basedir' => '/example_dir',
) );

WP_Mock::userFunction( 'delete_user_meta' )
->andReturn( true );

WP_Mock::userFunction( 'wp_get_attachment_url' )
->with( $url_or_media_id )
->andReturn( $meta_value['full'] );

WP_Mock::userFunction( 'get_current_blog_id' )
->andReturn( $meta_value['blog_id'] );

WP_Mock::userFunction( 'update_user_meta' )
->with( $user_id, 'simple_local_avatar', $meta_value )
->andReturn( 101 );

$this->instance->assign_new_user_avatar( $url_or_media_id, $user_id );
}

public function test_edit_user_profile_update() {
$user_id = 1;

$_POST['simple_local_avatar_rating'] = '';
$_POST['_simple_local_avatar_nonce'] = 'not empty';

WP_Mock::userFunction( 'wp_verify_nonce' )
->with( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' )
->andReturn( true );

WP_Mock::userFunction( 'update_user_meta' )
->with( $user_id, 'simple_local_avatar_rating', 'G' )
->andReturn( true );

$this->instance->edit_user_profile_update( $user_id );
}

public function test_action_remove_simple_local_avatar() {
$_GET['user_id'] = 1;
$_GET['_wpnonce'] = 1;

WP_Mock::userFunction( 'wp_verify_nonce' )
->with( $_GET['_wpnonce'], 'remove_simple_local_avatar_nonce' )
->andReturn( true );

WP_Mock::userFunction( 'wp_upload_dir' )
->andReturn( array(
'baseurl' => '/example',
'basedir' => '/example_dir',
) );

WP_Mock::userFunction( 'current_user_can' )
->with( 'edit_user', $_GET['user_id'] )
->andReturn( true );

$this->instance->action_remove_simple_local_avatar();
}

public function test_unique_filename_callback() {
$dir = '/test_dir';
$ext = '.ext';
$expected_file_name = 'SLA_FILE';

WP_Mock::userFunction( 'sanitize_file_name' )
->with( 'TEST_USER_avatar_' . time() )
->andReturn( $expected_file_name );

$name = $this->instance->unique_filename_callback( $dir, '', $ext );
$this->assertEquals( $expected_file_name . $ext, $name );
}

public function test_get_avatar_rest() {

$user = array( 'id' => 1 );

$expected_data = array(
'media_id' => 101,
'full' => 'https://example.com/avatar.png',
'96' => 'https://example.com/avatar-96x96.png',
);
$result = $this->instance->get_avatar_rest( $user );
$this->assertEquals( $expected_data, $result );
}

public function test_user_edit_form_tag() {
ob_start();
$this->instance->user_edit_form_tag();
Expand All @@ -222,18 +398,18 @@ public function test_user_edit_form_tag() {

public function test_upload_size_limit() {
WP_Mock::onFilter( 'simple_local_avatars_upload_limit' )
->with( 2048 )
->reply( 4096 );
->with( 2048 )
->reply( 4096 );
$this->assertEquals( 4096, $this->instance->upload_size_limit( 2048 ) );
}

public function test_avatar_delete() {
WP_Mock::userFunction( 'get_user_meta' )
->with( 1, 'simple_local_avatar', true )
->andReturn( [] );
->with( 1, 'simple_local_avatar', true )
->andReturn( [] );

WP_Mock::userFunction( 'delete_user_meta' )
->never();
->never();

$this->instance->avatar_delete( 1 );
}
Expand Down