diff --git a/tests/e2e/api/src/framework/transformations/model-transformer-transformation.ts b/tests/e2e/api/src/framework/transformations/model-transformer-transformation.ts index d8946043b79..13728a84508 100644 --- a/tests/e2e/api/src/framework/transformations/model-transformer-transformation.ts +++ b/tests/e2e/api/src/framework/transformations/model-transformer-transformation.ts @@ -56,8 +56,8 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT * @return {*} The transformed properties. */ public fromModel( properties: any ): any { - if ( properties.hasOwnProperty( this.property ) ) { - const val = properties[ this.property ]; + const val = properties[ this.property ]; + if ( val ) { if ( Array.isArray( val ) ) { properties[ this.property ] = val.map( ( v ) => this.transformer.fromModel( v ) ); } else { @@ -75,8 +75,8 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT * @return {*} The transformed properties. */ public toModel( properties: any ): any { - if ( properties.hasOwnProperty( this.property ) ) { - const val = properties[ this.property ]; + const val = properties[ this.property ]; + if ( val ) { if ( Array.isArray( val ) ) { properties[ this.property ] = val.map( ( v ) => this.transformer.toModel( this.modelClass, v ) ); } else { diff --git a/tests/e2e/api/src/models/products/abstract-product.ts b/tests/e2e/api/src/models/products/abstract-product.ts index bc2df02471e..8e3163b29ce 100644 --- a/tests/e2e/api/src/models/products/abstract-product.ts +++ b/tests/e2e/api/src/models/products/abstract-product.ts @@ -1,5 +1,5 @@ import { Model } from '../model'; -import { PostStatus } from '../shared-types'; +import { MetaData, PostStatus } from '../shared-types'; import { BackorderStatus, CatalogVisibility, @@ -335,4 +335,11 @@ export abstract class AbstractProduct extends Model { * @type {number} */ public readonly numRatings: number = -1; + + /** + * The extra metadata for the product. + * + * @type {ReadonlyArray.} + */ + public readonly metaData: readonly MetaData[] = []; } diff --git a/tests/e2e/api/src/models/products/shared-types.ts b/tests/e2e/api/src/models/products/shared-types.ts index f22972bb81c..68ed7d9a865 100644 --- a/tests/e2e/api/src/models/products/shared-types.ts +++ b/tests/e2e/api/src/models/products/shared-types.ts @@ -133,6 +133,7 @@ export class ProductDownload { /** * The URL of the downloadable file. * + * * @type {string} */ public readonly url: string = ''; @@ -179,6 +180,13 @@ export class ProductAttribute { */ public readonly isVisibleOnProductPage: boolean = false; + /** + * Indicates whether or not the attribute should be used in variations. + * + * @type {boolean} + */ + public readonly isForVariations: boolean = false; + /** * The options which are available for the attribute. * @@ -242,13 +250,6 @@ export class ProductImage { */ public readonly altText: string = ''; - /** - * The sort order for the image. - * - * @type {number} - */ - public readonly sortOrder: number = -1; - /** * Creates a new product image. * diff --git a/tests/e2e/api/src/models/shared-types.ts b/tests/e2e/api/src/models/shared-types.ts index c6173591dbc..5f64fff0b72 100644 --- a/tests/e2e/api/src/models/shared-types.ts +++ b/tests/e2e/api/src/models/shared-types.ts @@ -20,7 +20,7 @@ export type PostStatus = 'draft' | 'pending' | 'private' | 'publish' | string; /** * A metadata object. */ -export class MetaData { +export class MetaData extends Model { /** * The key of the metadata. * @@ -41,6 +41,7 @@ export class MetaData { * @param {Partial.} properties The properties to set. */ public constructor( properties?: Partial< MetaData > ) { + super(); Object.assign( this, properties ); } } diff --git a/tests/e2e/api/src/repositories/rest/products/shared.ts b/tests/e2e/api/src/repositories/rest/products/shared.ts index 50dfec2df99..bd0a6466583 100644 --- a/tests/e2e/api/src/repositories/rest/products/shared.ts +++ b/tests/e2e/api/src/repositories/rest/products/shared.ts @@ -8,13 +8,98 @@ import { PropertyTypeTransformation, } from '../../../framework/transformations/property-type-transformation'; import { CustomTransformation } from '../../../framework/transformations/custom-transformation'; +import { ProductAttribute, ProductDownload, ProductImage, ProductTerm } from '../../../models/products/shared-types'; +import { ModelTransformerTransformation } from '../../../framework/transformations/model-transformer-transformation'; +import { MetaData } from '../../../models/shared-types'; +import { createMetaDataTransformer } from '../shared'; + +/** + * Creates a transformer for the product term object. + * + * @return {ModelTransformer} The created transformer. + */ +function createProductTermTransformer(): ModelTransformer< ProductTerm > { + return new ModelTransformer( + [ + new PropertyTypeTransformation( { id: PropertyType.Integer } ), + ], + ); +} + +/** + * Creates a transformer for the product attribute object. + * + * @return {ModelTransformer} The created transformer. + */ +function createProductAttributeTransformer(): ModelTransformer< ProductAttribute > { + return new ModelTransformer( + [ + new PropertyTypeTransformation( + { + id: PropertyType.Integer, + sortOrder: PropertyType.Integer, + isVisibleOnProductPage: PropertyType.Boolean, + isForVariations: PropertyType.Boolean, + }, + ), + new KeyChangeTransformation< ProductAttribute >( + { + sortOrder: 'position', + isVisibleOnProductPage: 'visible', + isForVariations: 'variation', + }, + ), + ], + ); +} + +/** + * Creates a transformer for the product image object. + * + * @return {ModelTransformer} The created transformer. + */ +function createProductImageTransformer(): ModelTransformer< ProductImage > { + return new ModelTransformer( + [ + new IgnorePropertyTransformation( [ 'date_created', 'date_modified' ] ), + new PropertyTypeTransformation( + { + id: PropertyType.Integer, + created: PropertyType.Date, + modified: PropertyType.Date, + }, + ), + new KeyChangeTransformation< ProductImage >( + { + created: 'date_created_gmt', + modified: 'date_modified_gmt', + url: 'src', + altText: 'altText', + }, + ), + ], + ); +} + +/** + * Creates a transformer for the product download object. + * + * @return {ModelTransformer} The created transformer. + */ +function createProductDownloadTransformer(): ModelTransformer< ProductDownload > { + return new ModelTransformer( + [ + new KeyChangeTransformation< ProductDownload >( { url: 'file' } ), + ], + ); +} /** * Creates a transformer for the shared properties of all products. * * @param {string} type The product type. * @param {Array.} transformations Optional transformers to add to the transformer. - * @return {ModelTransformer} The created transform. + * @return {ModelTransformer} The created transformer. */ export function createProductTransformer< T extends AbstractProduct >( type: string, @@ -34,6 +119,12 @@ export function createProductTransformer< T extends AbstractProduct >( 'date_on_sale_to', ], ), + new ModelTransformerTransformation( 'categories', ProductTerm, createProductTermTransformer() ), + new ModelTransformerTransformation( 'tags', ProductTerm, createProductTermTransformer() ), + new ModelTransformerTransformation( 'attributes', ProductAttribute, createProductAttributeTransformer() ), + new ModelTransformerTransformation( 'images', ProductImage, createProductImageTransformer() ), + new ModelTransformerTransformation( 'downloads', ProductDownload, createProductDownloadTransformer() ), + new ModelTransformerTransformation( 'metaData', MetaData, createMetaDataTransformer() ), new CustomTransformation( TransformationOrder.Normal, ( properties: any ) => { @@ -121,6 +212,7 @@ export function createProductTransformer< T extends AbstractProduct >( allowReviews: 'reviews_allowed', averageRating: 'average_rating', numRatings: 'rating_count', + metaData: 'meta_data', }, ), ); diff --git a/tests/e2e/api/src/repositories/rest/shared.ts b/tests/e2e/api/src/repositories/rest/shared.ts new file mode 100644 index 00000000000..84f104f6bb1 --- /dev/null +++ b/tests/e2e/api/src/repositories/rest/shared.ts @@ -0,0 +1,16 @@ +import { ModelTransformer } from '../../framework/model-transformer'; +import { MetaData } from '../../models/shared-types'; +import { IgnorePropertyTransformation } from '../../framework/transformations/ignore-property-transformation'; + +/** + * Creates a new transformer for metadata models. + * + * @return {ModelTransformer} The created transformer. + */ +export function createMetaDataTransformer(): ModelTransformer< MetaData > { + return new ModelTransformer( + [ + new IgnorePropertyTransformation( [ 'id' ] ), + ], + ); +}