diff --git a/lib/helpers/ApiHelpers.ts b/lib/helpers/ApiHelpers.ts index 884eab8..e8c1847 100644 --- a/lib/helpers/ApiHelpers.ts +++ b/lib/helpers/ApiHelpers.ts @@ -137,9 +137,11 @@ export class ApiHelpers { } } - async get(url: string, params?: { [key: string]: string | number | boolean; }) { + async get(url: string, params?: { [key: string]: string | number | boolean; }, extraHeaders?: { [key: string]: string; }) { + const headers = await this.getHeaders(); + const allHeaders = { ...headers, ...extraHeaders }; const options = { - headers: await this.getHeaders(), + headers: allHeaders, params: params, ignoreHTTPSErrors: true } diff --git a/lib/helpers/DocumentApiHelper.ts b/lib/helpers/DocumentApiHelper.ts index aa51e2c..39b2c60 100644 --- a/lib/helpers/DocumentApiHelper.ts +++ b/lib/helpers/DocumentApiHelper.ts @@ -1491,7 +1491,7 @@ export class DocumentApiHelper { return false; } - async createDocumentWithTwoCulturesAndTextContent(documentName: string, documentTypeId: string, textContent: string, dataTypeName: string, firstCulture: string, secondCulture: string) { + async createDocumentWithTwoCulturesAndTextContent(documentName: string, documentTypeId: string, textContent: string, dataTypeName: string, firstCulture: string, secondCulture: string, firstDomainName: string = '/testfirstdomain', secondDomainName: string = '/testseconddomain') { await this.ensureNameNotExists(documentName); const document = new DocumentBuilder() @@ -1513,11 +1513,11 @@ export class DocumentApiHelper { const domainData = new DocumentDomainBuilder() .addDomain() - .withDomainName('/testfirstdomain') + .withDomainName(firstDomainName) .withIsoCode(firstCulture) .done() .addDomain() - .withDomainName('/testseconddomain') + .withDomainName(secondDomainName) .withIsoCode(secondCulture) .done() .build(); @@ -1589,4 +1589,53 @@ export class DocumentApiHelper { // Create document return await this.create(document); } + + async createVariantDocumentWithVariantProperty(documentName: string, documentTypeId: string, dataTypeName: string, propertyVariants: {culture: string, value}[]) { + await this.ensureNameNotExists(documentName); + + const documentDataBuilder = new DocumentBuilder() + .withDocumentTypeId(documentTypeId); + + for (const property of propertyVariants) { + documentDataBuilder + .addVariant() + .withName(property.culture === 'en-US' ? documentName : documentName + ' - ' + property.culture) + .withCulture(property.culture) + .done() + .addValue() + .withAlias(AliasHelper.toAlias(dataTypeName)) + .withValue(property.value) + .withCulture(property.culture) + .done(); + } + const document = documentDataBuilder.build(); + + return await this.create(document); + } + + async updateDomainsForVariantDocument(documentId: string, domains: {domainName: string, isoCode: string}[]) { + const domainDataBuilder = new DocumentDomainBuilder(); + for (const domain of domains) { + domainDataBuilder.addDomain() + .withDomainName(domain.domainName) + .withIsoCode(domain.isoCode) + .done(); + } + const domainData = domainDataBuilder.build(); + return await this.updateDomains(documentId, domainData); + } + + async addTextstringValueToInvariantDocument(documentId: string, dataTypeName: string, textValue: string) { + const documentData = await this.get(documentId); + const textValueAlias = AliasHelper.toAlias(dataTypeName); + documentData.values.push({ + alias: textValueAlias, + value: textValue, + culture: null, + segment: null, + editorAlias: 'Umbraco.Textbox', + entityType: 'document-property-value' + }); + return await this.update(documentId, documentData); + } } \ No newline at end of file diff --git a/lib/helpers/differentAppSettingsHelpers/ContentDeliveryApiHelper.ts b/lib/helpers/differentAppSettingsHelpers/ContentDeliveryApiHelper.ts index 70ef767..250e25f 100644 --- a/lib/helpers/differentAppSettingsHelpers/ContentDeliveryApiHelper.ts +++ b/lib/helpers/differentAppSettingsHelpers/ContentDeliveryApiHelper.ts @@ -1,4 +1,4 @@ -import {expect} from "@playwright/test"; +import {expect} from "@playwright/test"; import {ApiHelpers} from "../ApiHelpers"; export class ContentDeliveryApiHelper { @@ -13,23 +13,34 @@ export class ContentDeliveryApiHelper { return await response.json(); } - async getContentItemWithId(id: string) { - return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content/item/' + id); + async getContentItemWithId(id: string, extraHeaders?: { [key: string]: string; }, expand?: string, fields?: string) { + if (expand || fields) { + let query = '?'; + if (expand) { + query += 'expand=' + expand; + } + if (fields) { + query += (query.length > 1 ? '&' : '') + 'fields=' + fields; + } + return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content/item/' + id + query, undefined, extraHeaders); + } else { + return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content/item/' + id, undefined, extraHeaders); + } } - async getContentItemWithRoute(route: string) { - return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content/item' + route); + async getContentItemWithRoute(route: string, extraHeaders?: { [key: string]: string; }) { + return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content/item' + route, undefined, extraHeaders); } - async getContentItemsWithIds(ids: string[]) { + async getContentItemsWithIds(ids: string[], extraHeaders?: { [key: string]: string; }) { let query = '?'; for (let i = 0; i < ids.length; i++) { query += 'id=' + ids[i] + (i < ids.length - 1 ? '&' : ''); } - return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content/items' + query); + return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content/items' + query, undefined, extraHeaders); } - async getContentItemsFromAQuery(fetch?: string, filter?: string, sort?: string, skip?: number, take?: number) { + async getContentItemsFromAQuery(extraHeaders?: { [key: string]: string; }, fetch?: string, filter?: string, sort?: string, skip?: number, take?: number) { let query = ''; if (fetch) { query += ' fetch=' + fetch; @@ -50,7 +61,7 @@ export class ContentDeliveryApiHelper { query = '?' + query.trim().replace(' ', '&'); } - return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content' + query); + return await this.api.get(this.api.baseUrl + '/umbraco/delivery/api/v2/content' + query, undefined, extraHeaders); } async verifyBasicPropertiesForContentItem(contentName: string, contentItemJson) { @@ -63,22 +74,27 @@ export class ContentDeliveryApiHelper { expect(contentItemJson.id).toBe(contentData.id); const contentTypeData = await this.api.documentType.get(contentData.documentType.id); expect(contentItemJson.contentType).toBe(contentTypeData.alias); + } + async verifyRoutePropertyForContentItem(contentName: string, contentItemJson, expectedRoutePath?: string) { // Verify route property - const contentUrl = await this.api.document.getDocumentUrl(contentData.id); - expect(contentItemJson.route.path).toBe(contentUrl); + if (expectedRoutePath !== undefined) { + expect(contentItemJson.route.path).toBe(expectedRoutePath); + } else { + const contentData = await this.api.document.getByName(contentName); + const contentUrl = await this.api.document.getDocumentUrl(contentData.id); + expect(contentItemJson.route.path).toBe(contentUrl); + } } - async verifyEditorialPropertiesForContentItem(contentName: string, contentItemJson, isVariesByCulture: boolean = false) { + async verifyEditorialPropertiesForInvariantContentItem(contentName: string, contentItemJson) { const contentData = await this.api.document.getByName(contentName); let expectedProperties = {}; - if (!isVariesByCulture) { - for (const property of contentData.values) { - expectedProperties[property.alias] = property.value; - } - expect(contentItemJson.properties).toEqual(expectedProperties); + for (const property of contentData.values) { + expectedProperties[property.alias] = property.value; } + expect(contentItemJson.properties).toEqual(expectedProperties); } async verifyCulturePropertyForContentItem(contentName: string, contentItemJson, isVariesByCulture: boolean = false) { @@ -104,4 +120,19 @@ export class ContentDeliveryApiHelper { expect(actualMultiUrlPickerValue[0].route.path).toBe(expectedMultiUrlPickerValue[0].url); expect(actualMultiUrlPickerValue[0].linkType).toBe(pickerType); } + + async verifyPropertiesForMediaItem(mediaName: string, mediaItemJson) { + const mediaData = await this.api.media.getByName(mediaName); + + expect(mediaItemJson.name).toBe(mediaName); + expect(mediaItemJson.focalPoint).toEqual(mediaData.values[0].value.focalPoint); + expect(mediaItemJson.crops).toEqual(mediaData.values[0].value.crops); + expect(mediaItemJson.id).toEqual(mediaData.id); + const expectedWidthValue = mediaData.values.find((p) => p.alias === 'umbracoWidth').value; + expect(mediaItemJson.width).toEqual(Number(expectedWidthValue)); + const expectedHeightValue = mediaData.values.find((p) => p.alias === 'umbracoHeight').value; + expect(mediaItemJson.height).toEqual(Number(expectedHeightValue)); + const expectedBytesValue = mediaData.values.find((p) => p.alias === 'umbracoBytes').value; + expect(mediaItemJson.bytes).toEqual(Number(expectedBytesValue)); + } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 978c480..ae16ca1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@umbraco/playwright-testhelpers", - "version": "16.0.50", + "version": "16.0.51", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@umbraco/playwright-testhelpers", - "version": "16.0.50", + "version": "16.0.51", "license": "MIT", "dependencies": { "@umbraco/json-models-builders": "2.0.40", diff --git a/package.json b/package.json index 994200f..eef2544 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@umbraco/playwright-testhelpers", - "version": "16.0.50", + "version": "16.0.51", "description": "Test helpers for making playwright tests for Umbraco solutions", "main": "dist/lib/index.js", "files": [