Skip to content

test(e2e): Cover the media storage modes - #1366

Open
utkarshcloudinary wants to merge 2 commits into
developfrom
test/storage-modes-e2e
Open

utkarshcloudinary wants to merge 2 commits into
developfrom
test/storage-modes-e2e

Conversation

@utkarshcloudinary

Copy link
Copy Markdown
Collaborator

Adds an e2e spec for the three storage settings: "Cloudinary only", "Cloudinary and WordPress" and "Cloudinary and WordPress (low resolution)". No spec covered them before, and a bug here can remove the only local copy of a file.

Approach

  • cloudinary-storage-modes.spec.js (@serial) pins lazy load off and auto sync on, sets offload for each test, and restores the settings in afterAll.
  • "Cloudinary only": after the first sync the local file stays and the delete delay starts. The spec moves the delay start back instead of waiting Storage::DELETE_DELAY, syncs again, and checks that the main file and every size file are removed and _cloudinary_storage is cld. The image must still load from Cloudinary.
  • "Cloudinary and WordPress": the same attachment is synced back. The local file must have the same size as the uploaded original, and every size file must be generated again.
  • Low resolution: a new upload keeps a local file that is smaller than the original, and delivery still uses Cloudinary.
  • The spec syncs only its own attachments through process_assets(), not wp cloudinary sync. A storage change replaces or removes the local files of every synced attachment, so a library-wide sync changes media that other specs or developers left in the test site.
  • afterAll removes the Cloudinary assets through the API before it deletes the attachments. Delete sync removes an asset only when Sync::is_synced() is true, and after a storage change it is not, so the assets stayed in the account.

QA notes

  • npm run test:e2e:serial -- cloudinary-storage-modes
  • After the run, the Cloudinary account has no storage-* assets, and cloudinary_sync_media.offload is back to its value before the run.

Add an e2e spec for the three storage settings. It checks that
"Cloudinary only" keeps the local files during the delete delay and
removes them after it, that "Cloudinary and WordPress" downloads the
full-quality files again, and that the low resolution mode keeps a
smaller local copy. In each mode the image must still load from
Cloudinary.

Cleanup removes the Cloudinary assets through the API. Deleting an
attachment removes its asset only when the plugin thinks it is synced,
and after a storage change it is not.

@gabrielcld2 gabrielcld2 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of suggestions and nitpicks but otherwise it looks good 👍

$api->destroy(
$media->get_resource_type( $id ),
array(
'public_id' => $media->get_public_id( $id ),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Delete_Sync::delete_asset() calls get_public_id( $id, true ) to get the suffixed public ID. With the timestamped filenames a suffix is unlikely, but passing true here too would make sure we destroy the same asset the plugin would.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d8df727: it now uses get_public_id( $id, true ).

'exists' => file_exists( $file ),
'bytes' => file_exists( $file ) ? filesize( $file ) : 0,
'sizes' => $sizes,
'storage' => (string) $media->get_post_meta( $id, Sync::META_KEYS['storage'], true ),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: could storageState() also return the sync_error meta, with an assertion after each sync() that it's empty? Right now process_assets() can fail without any error the spec sees. The test would then fail later on "the delay should start" or the file checks, which doesn't point to the real cause.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d8df727. I put the check in sync() instead of storageState(): it reads the sync_error meta right after process_assets() and fails with "attachment N should sync" and the error text. So every sync is checked, and the failure points to the real cause.

expect(
state.bytes,
'the local file should be the low-quality version'
).toBeLessThan( FIXTURE_BYTES );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: the other two tests check the size files, but this one doesn't. Should we also check that they're generated again from the low-resolution download?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d8df727: the low resolution test now checks that every size file exists after the download.

Fail at once when a sync saves an error, check the size files in the
low resolution mode, and destroy the suffixed public ID as delete sync
does.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants