Skip to content

Commit

Permalink
Merge branch 'main' into add/10843-jest-v28
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed May 23, 2022
2 parents b6d6b5f + 195a354 commit efbe101
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 19 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"psr-4": {
"Google\\Web_Stories\\Tests\\Integration\\": "tests/phpunit/integration/includes",
"Google\\Web_Stories\\Tests\\Unit\\": "tests/phpunit/unit/includes",
"Google\\Web_Stories\\Tests\\Shared\\": "tests/phpunit/shared",
"Google\\Web_Stories\\PHPStan\\": "tests/phpstan/src"
}
},
Expand Down
2 changes: 1 addition & 1 deletion includes/Shopping/Shopify_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected function get_products_query( string $search_term ): string {
images(first: 10) {
edges {
node {
url
url(transform:{maxWidth:1000,maxHeight:1000})
altText
}
}
Expand Down
35 changes: 30 additions & 5 deletions includes/Shopping/Woocommerce_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,22 @@ public function get_search( string $search_term ) {

$product_image_ids = [];
foreach ( $products as $product ) {
$product_image_ids[] = $product->get_gallery_image_ids();
$product_image_ids[] = $this->get_product_image_ids( $product );
}
$products_image_ids = array_merge( [], ...$product_image_ids );

/**
* Warm the object cache with post and meta information for all found
* images to avoid making individual database calls.
*/
_prime_post_caches( $products_image_ids, false, true );

foreach ( $products as $product ) {
$product_image_ids = array_map( 'absint', $product->get_gallery_image_ids() );
$images = array_map( [ $this, 'get_product_images' ], $product_image_ids );

$images = array_map(
[ $this, 'get_product_image' ],
$this->get_product_image_ids( $product )
);

$product_object = new Product(
[
Expand All @@ -104,6 +108,21 @@ public function get_search( string $search_term ) {
return $results;
}

/**
* Get all product image ids (feature image + gallery_images).
*
* @since 1.21.0
*
* @param \WC_Product $product Product.
* @return array
*/
protected function get_product_image_ids( $product ): array {
$product_image_ids = $product->get_gallery_image_ids();
array_unshift( $product_image_ids, $product->get_image_id() );
$product_image_ids = array_map( 'absint', $product_image_ids );
return array_unique( array_filter( $product_image_ids ) );
}

/**
* Get product image, url and alt.
*
Expand All @@ -112,9 +131,15 @@ public function get_search( string $search_term ) {
* @param int $image_id Attachment ID.
* @return array
*/
protected function get_product_images( int $image_id ): array {
$url = wp_get_attachment_url( $image_id );
protected function get_product_image( int $image_id ): array {
$url = wp_get_attachment_image_url( $image_id, 'large' );

if ( ! $url ) {
return [];
}

$alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );

if ( empty( $alt ) ) {
$alt = '';
}
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 26 additions & 6 deletions packages/e2e-tests/src/specs/editor/shopping/woocommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,19 @@ describe('Shopping', () => {
expect(item).toMatchSchema(schema);
});

// Since WooCommerce product IDs can change between test runs / setups,
// this changes them to something deterministic.

// Since product IDs and attachment URLs can change between test runs / setups,
// this changes them to something deterministic for the sake of this snapshot.
// Note: Could also be done with a custom snapshot serializer.
const normalizedItems = items.map((item) => ({
...item,
productId: 'product-id',
productImages: item.productImages.map((image) => ({
...image,
url: 'product-image-url',
})),
}));

// eslint-disable-next-line jest/no-large-snapshots
expect(normalizedItems).toMatchInlineSnapshot(`
Array [
Object {
Expand All @@ -89,7 +94,12 @@ describe('Shopping', () => {
"productBrand": "",
"productDetails": "This is a simple product.",
"productId": "product-id",
"productImages": Array [],
"productImages": Array [
Object {
"alt": "",
"url": "product-image-url",
},
],
"productPrice": 45,
"productPriceCurrency": "USD",
"productTitle": "Hoodie with Zipper",
Expand All @@ -104,7 +114,12 @@ describe('Shopping', () => {
"productBrand": "",
"productDetails": "This is a simple, virtual product.",
"productId": "product-id",
"productImages": Array [],
"productImages": Array [
Object {
"alt": "",
"url": "product-image-url",
},
],
"productPrice": 15,
"productPriceCurrency": "USD",
"productTitle": "Album",
Expand All @@ -119,7 +134,12 @@ describe('Shopping', () => {
"productBrand": "",
"productDetails": "This is a simple product.",
"productId": "product-id",
"productImages": Array [],
"productImages": Array [
Object {
"alt": "",
"url": "product-image-url",
},
],
"productPrice": 90,
"productPriceCurrency": "USD",
"productTitle": "Sunglasses",
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/integration/includes/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Tests\Shared\Private_Access;
use Yoast\WPTestUtils\WPIntegration\TestCase as PolyfilledTestCase;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

namespace Google\Web_Stories\Tests\Integration;
namespace Google\Web_Stories\Tests\Shared;

use ReflectionClass;
use ReflectionException;
Expand Down
74 changes: 74 additions & 0 deletions tests/phpunit/unit/tests/Shopping/Mock_Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Web_Stories\Tests\Unit\Shopping;

/**
* Class Mock_Product.
* Mocks WooCommerce product methods as needed unit tests.
*/
class Mock_Product {

/**
* @param mixed $data
*/
public function __construct( $product_data ) {
$this->data = $product_data;
}

/**
* handle calls for methods that haven't been mocked
*/
/**
* @param mixed $method_name
* @param mixed $method_args
*/
public function __call( $method_name, $method_args ): void {
// no-op
}

/**
* Pulls a prop from the data array if it exists
*
* @param mixed $prop
* @return mixed
*/
public function get_prop( $prop ) {
$value = null;

if ( \array_key_exists( $prop, $this->data ) ) {
$value = \array_key_exists( $prop, $this->data ) ? $this->data[ $prop ] : null;
}

return $value;
}

public function get_id(): ?int {
return $this->get_prop( 'id' );
}

public function get_image_id(): ?int {
return $this->get_prop( 'image_id' );
}

/**
* @return array|null
*/
public function get_gallery_image_ids(): ?array {
return $this->get_prop( 'gallery_image_ids' );
}
}

0 comments on commit efbe101

Please sign in to comment.