UNPKG

@umbraco/playwright-testhelpers

Version:

Test helpers for making playwright tests for Umbraco solutions

243 lines 10.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentBlueprintApiHelper = void 0; const json_models_builders_1 = require("@umbraco/json-models-builders"); class DocumentBlueprintApiHelper { api; constructor(api) { this.api = api; } async get(id) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/document-blueprint/' + id); return await response.json(); } async doesExist(id) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/document-blueprint/' + id); return response.status() === 200; } async create(documentBlueprint) { if (documentBlueprint == null) { return; } const response = await this.api.post(this.api.baseUrl + '/umbraco/management/api/v1/document-blueprint', documentBlueprint); return response.headers().location.split("v1/document-blueprint/").pop(); } async delete(id) { if (id == null) { return; } const response = await this.api.delete(this.api.baseUrl + '/umbraco/management/api/v1/document-blueprint/' + id); return response.status(); } async update(id, documentBlueprint) { if (documentBlueprint == null) { return; } return await this.api.put(this.api.baseUrl + '/umbraco/management/api/v1/document-blueprint/' + id, documentBlueprint); } async getAllAtRoot() { return await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/tree/document-blueprint/root?skip=0&take=1000&foldersOnly=false'); } async getChildren(id) { const response = await this.api.get(`${this.api.baseUrl}/umbraco/management/api/v1/tree/document-blueprint/children?parentId=${id}&skip=0&take=10000&foldersOnly=false`); const items = await response.json(); return items.items; } async doesNameExist(name) { return await this.getByName(name); } async recurseDeleteChildren(id) { const items = await this.getChildren(id); for (const child of items) { if (child.hasChildren) { await this.recurseDeleteChildren(child.id); } else { await this.delete(child.id); } } return await this.delete(id); } async recurseChildren(name, id, toDelete) { const items = await this.getChildren(id); for (const child of items) { if (child.name === name) { if (!toDelete) { return await this.get(child.id); } if (child.hasChildren) { return await this.recurseDeleteChildren(child.id); } else { return await this.delete(child.id); } } else if (child.hasChildren) { await this.recurseChildren(name, child.id, toDelete); } } return false; } async getByName(name) { const rootDocumentBlueprints = await this.getAllAtRoot(); const jsonDocumentBlueprints = await rootDocumentBlueprints.json(); for (const blueprint of jsonDocumentBlueprints.items) { if (blueprint.name === name) { return this.get(blueprint.id); } else if (blueprint.hasChildren) { const result = await this.recurseChildren(name, blueprint.id, false); if (result) { return result; } } } return false; } async ensureNameNotExists(name) { const rootDocumentBlueprints = await this.getAllAtRoot(); const jsonDocumentBlueprints = await rootDocumentBlueprints.json(); for (const blueprint of jsonDocumentBlueprints.items) { if (blueprint.name === name) { if (blueprint.hasChildren) { await this.recurseDeleteChildren(blueprint.id); } return await this.delete(blueprint.id); } else if (blueprint.hasChildren) { await this.recurseChildren(name, blueprint.id, true); } } return null; } async createDefaultDocumentBlueprint(documentBlueprintName, documentTypeId) { await this.ensureNameNotExists(documentBlueprintName); const documentBlueprint = new json_models_builders_1.DocumentBlueprintsBuilder() .withDocumentTypeId(documentTypeId) .addVariant() .withName(documentBlueprintName) .done() .build(); return await this.create(documentBlueprint); } async createFromDocument(documentBlueprintName, documentId, parentId) { const documentBlueprintData = { "name": documentBlueprintName, "parent": parentId ? parentId : null, "document": { "id": documentId } }; const response = await this.api.post(this.api.baseUrl + '/umbraco/management/api/v1/document-blueprint/from-document', documentBlueprintData); return response.headers().location.split("v1/document-blueprint/").pop(); } async createDocumentBlueprintWithTextBoxValue(documentBlueprintName, documentTypeId, dataTypeName, text) { await this.ensureNameNotExists(documentBlueprintName); const documentBlueprint = new json_models_builders_1.DocumentBlueprintsBuilder() .withDocumentTypeId(documentTypeId) .addVariant() .withName(documentBlueprintName) .done() .addValue() .withAlias(json_models_builders_1.AliasHelper.toAlias(dataTypeName)) .withValue(text) .withEditorAlias('Umbraco.TextBox') .done() .build(); return await this.create(documentBlueprint); } async createDefaultDocumentBlueprintWithABlockListEditorAndBlockWithValue(documentBlueprintName, documentTypeName, blockListDataTypeName, elementTypeId, elementTypePropertyAlias, elementTypePropertyEditorAlias, elementTypePropertyValue, groupName) { const crypto = require('crypto'); const blockContentKey = crypto.randomUUID(); const blockListDataTypeId = await this.api.dataType.createBlockListDataTypeWithABlock(blockListDataTypeName, elementTypeId) || ''; const documentTypeId = await this.api.documentType.createDocumentTypeWithPropertyEditor(documentTypeName, blockListDataTypeName, blockListDataTypeId, groupName) || ''; await this.ensureNameNotExists(documentBlueprintName); const documentBlueprint = new json_models_builders_1.DocumentBlueprintsBuilder() .withDocumentTypeId(documentTypeId) .addVariant() .withName(documentBlueprintName) .done() .addValue() .withAlias(json_models_builders_1.AliasHelper.toAlias(blockListDataTypeName)) .withEditorAlias('Umbraco.BlockList') .addBlockListValue() .addContentData() .withContentTypeKey(elementTypeId) .withKey(blockContentKey) .addContentDataValue() .withAlias(elementTypePropertyAlias) .withEditorAlias(elementTypePropertyEditorAlias) .withValue(elementTypePropertyValue) .done() .done() .addExpose() .withContentKey(blockContentKey) .done() .addLayout() .withContentKey(blockContentKey) .done() .done() .done() .build(); return await this.create(documentBlueprint); } async createDefaultDocumentBlueprintWithABlockGridEditorAndBlockWithValue(documentBlueprintName, documentTypeName, blockGridDataTypeName, elementTypeId, elementTypePropertyAlias, elementTypePropertyEditorAlias, elementTypePropertyValue, groupName = 'TestGroup') { const crypto = require('crypto'); const blockContentKey = crypto.randomUUID(); const blockGridDataTypeId = await this.api.dataType.createBlockGridWithABlockAndAllowAtRoot(blockGridDataTypeName, elementTypeId, true) || ''; const documentTypeId = await this.api.documentType.createDocumentTypeWithPropertyEditor(documentTypeName, blockGridDataTypeName, blockGridDataTypeId, groupName) || ''; await this.ensureNameNotExists(documentBlueprintName); const documentBlueprint = new json_models_builders_1.DocumentBlueprintsBuilder() .withDocumentTypeId(documentTypeId) .addVariant() .withName(documentBlueprintName) .done() .addValue() .withAlias(json_models_builders_1.AliasHelper.toAlias(blockGridDataTypeName)) .withEditorAlias('Umbraco.BlockGrid') .addBlockGridValue() .addContentData() .withContentTypeKey(elementTypeId) .withKey(blockContentKey) .addContentDataValue() .withAlias(elementTypePropertyAlias) .withEditorAlias(elementTypePropertyEditorAlias) .withValue(elementTypePropertyValue) .done() .done() .addExpose() .withContentKey(blockContentKey) .done() .addLayout() .withContentKey(blockContentKey) .done() .done() .done() .build(); return await this.create(documentBlueprint); } async createDocumenBlueprintWithEnglishCultureAndDanishCultureAndTextBoxValue(documentBlueprintEnglishName, documentBlueprintDanishName, documentTypeId, dataTypeName, textContent, varyByCultureForText = false) { await this.ensureNameNotExists(documentBlueprintEnglishName); const cultureValue = varyByCultureForText ? 'en-US' : null; const documentBlueprint = new json_models_builders_1.DocumentBlueprintsBuilder() .withDocumentTypeId(documentTypeId) .addVariant() .withName(documentBlueprintEnglishName) .withCulture('en-US') .done() .addVariant() .withName(documentBlueprintDanishName) .withCulture('da') .done() .addValue() .withAlias(json_models_builders_1.AliasHelper.toAlias(dataTypeName)) .withValue(textContent) .withCulture(cultureValue) .withEditorAlias('Umbraco.TextBox') .done() .build(); return await this.create(documentBlueprint); } } exports.DocumentBlueprintApiHelper = DocumentBlueprintApiHelper; //# sourceMappingURL=DocumentBlueprintApiHelper.js.map