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
178 changes: 178 additions & 0 deletions tests/phpunit/Unit/Modules/MediaLibrary/AdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
/**
* Tests for the MediaLibrary\Admin class.
*
* @package OneMedia\Tests\Unit\Modules\MediaLibrary
*/

declare( strict_types = 1 );

namespace OneMedia\Tests\Unit\Modules\MediaLibrary;

use OneMedia\Modules\MediaLibrary\Admin;
use OneMedia\Modules\MediaSharing\Attachment;
use OneMedia\Tests\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* Tests for the MediaLibrary\Admin class.
*/
#[CoversClass( Admin::class )]
final class AdminTest extends TestCase {
/**
* Original $_REQUEST state, restored after each test.
*
* @var array<string, mixed>
*/
private array $original_request;

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();

$this->original_request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}

/**
* {@inheritDoc}
*/
protected function tearDown(): void {
$_REQUEST = $this->original_request; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

parent::tearDown();
}

/**
* Tests no errors on hook registration.
*/
public function test_register_hooks_adds_expected_hooks(): void {
$admin = new Admin();
$admin->register_hooks();

$this->assertTrue( true );
}

/**
* Tests filter_ajax_query_attachments_args returns unchanged when meta_query is already set.
*/
public function test_filter_ajax_query_attachments_args_passthrough_when_meta_query_present(): void {
$admin = new Admin();
$query = [
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
[
'key' => 'custom_key',
'value' => '1',
'compare' => '=',
],
],
];

$this->assertSame( $query, $admin->filter_ajax_query_attachments_args( $query ) );
}

/**
* Tests filter_ajax_query_attachments_args returns unchanged when no sync filter is in the request.
*/
public function test_filter_ajax_query_attachments_args_passthrough_without_sync_filter(): void {
$admin = new Admin();
$query = [ 'post_type' => 'attachment' ];

$this->assertSame( $query, $admin->filter_ajax_query_attachments_args( $query ) );
}

/**
* Tests filter_ajax_query_attachments_args adds sync meta_query for onemedia_sync_status = sync.
*/
public function test_filter_ajax_query_attachments_args_adds_meta_query_for_sync_status(): void {
$_REQUEST['query'] = [ 'onemedia_sync_status' => Attachment::SYNC_STATUS_SYNC ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$admin = new Admin();
$result = $admin->filter_ajax_query_attachments_args( [ 'post_type' => 'attachment' ] );

$this->assertSame(
[
[
'key' => Attachment::IS_SYNC_POSTMETA_KEY,
'value' => '1',
'compare' => '=',
],
],
$result['meta_query'] // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
);
}

/**
* Tests filter_ajax_query_attachments_args adds no_sync meta_query for onemedia_sync_status = no_sync.
*/
public function test_filter_ajax_query_attachments_args_adds_meta_query_for_no_sync_status(): void {
$_REQUEST['query'] = [ 'onemedia_sync_status' => Attachment::SYNC_STATUS_NO_SYNC ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$admin = new Admin();
$result = $admin->filter_ajax_query_attachments_args( [ 'post_type' => 'attachment' ] );

$this->assertSame(
[
'relation' => 'OR',
[
'key' => Attachment::IS_SYNC_POSTMETA_KEY,
'value' => '0',
'compare' => '=',
],
[
'key' => Attachment::IS_SYNC_POSTMETA_KEY,
'compare' => 'NOT EXISTS',
],
],
$result['meta_query'] // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
);
}

/**
* Tests filter_ajax_query_attachments_args adds sync meta_query for is_onemedia_sync = true.
*/
public function test_filter_ajax_query_attachments_args_adds_meta_query_for_is_onemedia_sync_true(): void {
$_REQUEST['query'] = [ 'is_onemedia_sync' => 'true' ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$admin = new Admin();
$result = $admin->filter_ajax_query_attachments_args( [ 'post_type' => 'attachment' ] );

$this->assertSame(
[
[
'key' => Attachment::IS_SYNC_POSTMETA_KEY,
'value' => '1',
'compare' => '=',
],
],
$result['meta_query'] // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
);
}

/**
* Tests filter_ajax_query_attachments_args adds no_sync meta_query for is_onemedia_sync = false.
*/
public function test_filter_ajax_query_attachments_args_adds_meta_query_for_is_onemedia_sync_false(): void {
$_REQUEST['query'] = [ 'is_onemedia_sync' => 'false' ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$admin = new Admin();
$result = $admin->filter_ajax_query_attachments_args( [ 'post_type' => 'attachment' ] );

$this->assertSame(
[
'relation' => 'OR',
[
'key' => Attachment::IS_SYNC_POSTMETA_KEY,
'value' => '0',
'compare' => '=',
],
[
'key' => Attachment::IS_SYNC_POSTMETA_KEY,
'compare' => 'NOT EXISTS',
],
],
$result['meta_query'] // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
);
}
}
103 changes: 103 additions & 0 deletions tests/phpunit/Unit/Modules/MediaLibrary/ConsumerAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Tests for the MediaLibrary\ConsumerAdmin class.
*
* @package OneMedia\Tests\Unit\Modules\MediaLibrary
*/

declare( strict_types = 1 );

namespace OneMedia\Tests\Unit\Modules\MediaLibrary;

use OneMedia\Modules\MediaLibrary\ConsumerAdmin;
use OneMedia\Modules\MediaSharing\Attachment;
use OneMedia\Modules\Settings\Settings;
use OneMedia\Tests\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* Tests for the MediaLibrary\ConsumerAdmin class.
*/
#[CoversClass( ConsumerAdmin::class )]
final class ConsumerAdminTest extends TestCase {
/**
* Attachment post for testing.
*
* @var \WP_Post
*/
private \WP_Post $attachment;

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();

$this->attachment = get_post( self::factory()->attachment->create() );
}

/**
* {@inheritDoc}
*/
protected function tearDown(): void {
delete_option( Settings::OPTION_SITE_TYPE );

parent::tearDown();
}

/**
* Tests no errors on hook registration when not on a governing site.
*/
public function test_register_hooks_adds_expected_hooks(): void {
$consumer_admin = new ConsumerAdmin();
$consumer_admin->register_hooks();

$this->assertTrue( true );
}

/**
* Tests register_hooks skips all hooks when on a governing site.
*/
public function test_register_hooks_skips_hooks_on_governing_site(): void {
update_option( Settings::OPTION_SITE_TYPE, Settings::SITE_TYPE_GOVERNING );

$consumer_admin = new ConsumerAdmin();
$consumer_admin->register_hooks();

$this->assertFalse( has_filter( 'delete_attachment', [ $consumer_admin, 'prevent_attachment_deletion' ] ) );
}

/**
* Tests remove_edit_delete_links removes edit and delete actions for a synced attachment.
*/
public function test_remove_edit_delete_links_removes_actions_for_synced(): void {
Attachment::set_is_synced( $this->attachment->ID, true );

$consumer_admin = new ConsumerAdmin();
$result = $consumer_admin->remove_edit_delete_links(
[
'edit' => '<a>Edit</a>',
'delete' => '<a>Delete</a>',
],
$this->attachment
);

$this->assertArrayNotHasKey( 'edit', $result );
$this->assertArrayNotHasKey( 'delete', $result );
}

/**
* Tests remove_edit_delete_links keeps all actions for a non-synced attachment.
*/
public function test_remove_edit_delete_links_keeps_actions_for_non_synced(): void {
$consumer_admin = new ConsumerAdmin();
$actions = [
'edit' => '<a>Edit</a>',
'delete' => '<a>Delete</a>',
];

$result = $consumer_admin->remove_edit_delete_links( $actions, $this->attachment );

$this->assertSame( $actions, $result );
}
}
108 changes: 108 additions & 0 deletions tests/phpunit/Unit/Modules/MediaSharing/AttachmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Tests for the MediaSharing\Attachment class.
*
* @package OneMedia\Tests\Unit\Modules\MediaSharing
*/

declare( strict_types = 1 );

namespace OneMedia\Tests\Unit\Modules\MediaSharing;

use OneMedia\Modules\MediaSharing\Attachment;
use OneMedia\Modules\Settings\Settings;
use OneMedia\Tests\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* Tests for the MediaSharing\Attachment class.
*/
#[CoversClass( Attachment::class )]
final class AttachmentTest extends TestCase {
/**
* Attachment ID for testing.
*
* @var int
*/
private int $attachment_id;

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();

$this->attachment_id = self::factory()->attachment->create();
}

/**
* Tests no errors on hook registration.
*/
public function test_register_hooks_adds_expected_hooks(): void {
$attachment = new Attachment();
$attachment->register_hooks();

$this->assertTrue( true );
}

/**
* Tests is_sync_attachment returns false for a new attachment with no meta.
*/
public function test_is_sync_attachment_returns_false_for_new_attachment(): void {
$this->assertFalse( Attachment::is_sync_attachment( $this->attachment_id ) );
}

/**
* Tests set_is_synced and is_sync_attachment round-trip.
*/
public function test_set_is_synced_and_is_sync_attachment(): void {
Attachment::set_is_synced( $this->attachment_id, true );
$this->assertTrue( Attachment::is_sync_attachment( $this->attachment_id ) );

Attachment::set_is_synced( $this->attachment_id, false );
$this->assertFalse( Attachment::is_sync_attachment( $this->attachment_id ) );
}

/**
* Tests get_sync_sites returns empty array when not on a governing site.
*/
public function test_get_sync_sites_returns_empty_when_not_governing(): void {
$this->assertSame( [], Attachment::get_sync_sites( $this->attachment_id ) );
}

/**
* Tests get_sync_sites returns empty array on governing site when no meta is stored.
*/
public function test_get_sync_sites_returns_empty_on_governing_when_no_meta(): void {
update_option( Settings::OPTION_SITE_TYPE, Settings::SITE_TYPE_GOVERNING );

$this->assertSame( [], Attachment::get_sync_sites( $this->attachment_id ) );

delete_option( Settings::OPTION_SITE_TYPE );
}

/**
* Tests update_sync_attachment_versions and get_sync_attachment_versions round-trip.
*/
public function test_update_and_get_sync_attachment_versions(): void {
$versions = [
[
'last_used' => 1000000,
'file' => [
'path' => '/var/www/html/wp-content/uploads/test.jpg',
'url' => 'https://example.com/wp-content/uploads/test.jpg',
],
],
];

$this->assertTrue( Attachment::update_sync_attachment_versions( $this->attachment_id, $versions ) );
$this->assertSame( $versions, Attachment::get_sync_attachment_versions( $this->attachment_id ) );
}

/**
* Tests get_sync_attachment_versions returns empty array when no versions are stored.
*/
public function test_get_sync_attachment_versions_returns_empty_when_not_set(): void {
$this->assertSame( [], Attachment::get_sync_attachment_versions( $this->attachment_id ) );
}
}
Loading
Loading