Skip to content

Commit

Permalink
EW-798 expanded cc export of rich text elements of column board (#4923)
Browse files Browse the repository at this point in the history
* EW-798 implemented cc export for card content of column board
  • Loading branch information
Fshmit authored Apr 15, 2024
1 parent 6045fa3 commit 382839a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { ComponentProperties, ComponentType } from '@shared/domain/entity';
import { courseFactory, lessonFactory, setupEntities, taskFactory, userFactory } from '@shared/testing';
import { RichTextElement, RichTextElementProps } from '@shared/domain/domainobject';
import { InputFormat } from '@shared/domain/types';
import { LearnroomConfig } from '../learnroom.config';
import { CommonCartridgeMapper } from './common-cartridge.mapper';

Expand Down Expand Up @@ -397,4 +399,37 @@ describe('CommonCartridgeMapper', () => {
});
});
});

describe('mapRichTextElementToResource', () => {
describe('when mapping rich text element', () => {
const setup = () => {
const richTextElementProps: RichTextElementProps = {
text: faker.lorem.paragraph(),
inputFormat: InputFormat.RICH_TEXT_CK5,
id: faker.string.uuid(),
createdAt: faker.date.recent(),
updatedAt: faker.date.recent(),
};
const richTextElement = new RichTextElement(richTextElementProps);

configServiceMock.getOrThrow.mockReturnValue(faker.internet.url());

return { richTextElement };
};

it('should map to web content', () => {
const { richTextElement } = setup();

const resourceProps = sut.mapRichTextElementToResource(richTextElement);

expect(resourceProps).toStrictEqual<CommonCartridgeResourceProps>({
type: CommonCartridgeResourceType.WEB_CONTENT,
identifier: expect.any(String),
title: richTextElement.text.slice(0, 50).concat('...'),
html: `<p>${richTextElement.text}</p>`,
intendedUse: CommonCartridgeIntendedUseType.UNSPECIFIED,
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ComponentProperties, ComponentType, Course, LessonEntity, Task } from '@shared/domain/entity';
import { RichTextElement } from '@shared/domain/domainobject';
import { LearnroomConfig } from '../learnroom.config';

@Injectable()
Expand Down Expand Up @@ -117,4 +118,22 @@ export class CommonCartridgeMapper {
identifier: createIdentifier(course.id),
};
}

public mapRichTextElementToResource(element: RichTextElement): CommonCartridgeResourceProps {
return {
type: CommonCartridgeResourceType.WEB_CONTENT,
title: this.getTextTitle(element.text),
identifier: createIdentifier(element.id),
html: `<p>${element.text}</p>`,
intendedUse: CommonCartridgeIntendedUseType.UNSPECIFIED,
};
}

private getTextTitle(text: string): string {
const title = text
.slice(0, 50)
.replace(/<[^>]*>?/gm, '')
.concat('...');
return title;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
lessonFactory,
setupEntities,
taskFactory,
richTextElementFactory,
} from '@shared/testing';
import { ColumnBoardService } from '@src/modules/board';
import AdmZip from 'adm-zip';
Expand Down Expand Up @@ -71,7 +72,8 @@ describe('CommonCartridgeExportService', () => {
});
const [lesson] = lessons;
const taskFromLesson = taskFactory.buildWithId({ course, lesson });
const card = cardFactory.build();
const textCardElement = richTextElementFactory.build();
const card = cardFactory.build({ children: [textCardElement] });
const column = columnFactory.build({ children: [card] });
const columnBoard = columnBoardFactory.build({ children: [column] });

Expand All @@ -92,7 +94,7 @@ describe('CommonCartridgeExportService', () => {
);
const archive = new AdmZip(buffer);

return { archive, course, lessons, tasks, taskFromLesson, columnBoard, column, card };
return { archive, course, lessons, tasks, taskFromLesson, columnBoard, column, card, textCardElement };
};

beforeAll(async () => {
Expand Down Expand Up @@ -260,6 +262,13 @@ describe('CommonCartridgeExportService', () => {

expect(manifest).toContain(createXmlString('title', card.title));
});

it('should add content element of cards', async () => {
const { archive, textCardElement } = await setup();
const manifest = getFileContent(archive, 'imsmanifest.xml');

expect(manifest).toContain(`<resource identifier="i${textCardElement.id}"`);
});
});

describe('When topics array is empty', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { LessonService } from '@modules/lesson';
import { TaskService } from '@modules/task';
import { Injectable } from '@nestjs/common';
import { BoardExternalReferenceType, Column, Card } from '@shared/domain/domainobject';
import { BoardExternalReferenceType, Column, Card, RichTextElement } from '@shared/domain/domainobject';
import { ComponentProperties } from '@shared/domain/entity';
import { EntityId } from '@shared/domain/types';
import { ColumnBoardService } from '@src/modules/board';
Expand Down Expand Up @@ -129,10 +129,16 @@ export class CommonCartridgeExportService {

private addCardToOrganization(card: Card, organizationBuilder: CommonCartridgeOrganizationBuilder): void {
const { id } = card;
organizationBuilder.addSubOrganization({
const cardOrganization = organizationBuilder.addSubOrganization({
title: card.title,
identifier: createIdentifier(id),
});

card.children
.filter((child) => child instanceof RichTextElement)
.forEach((child) =>
cardOrganization.addResource(this.commonCartridgeMapper.mapRichTextElementToResource(child as RichTextElement))
);
}

private addComponentToOrganization(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Card, CardProps } from '@shared/domain/domainobject';
import { ObjectId } from '@mikro-orm/mongodb';
import { BaseFactory } from '../../base.factory';

export const cardFactory = BaseFactory.define<Card, CardProps>(Card, ({ sequence }) => {
export const cardFactory = BaseFactory.define<Card, CardProps>(Card, ({ sequence, params }) => {
return {
id: new ObjectId().toHexString(),
title: `card #${sequence}`,
height: 150,
children: [],
children: params?.children || [],
createdAt: new Date(),
updatedAt: new Date(),
};
Expand Down

0 comments on commit 382839a

Please sign in to comment.