Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/production' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Nov 22, 2023
2 parents c20096d + 44f0e79 commit 00832c0
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/api/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ export default {

async addLanguage(language, limit = 50) {
const [languageTranslationAlreadyExists] = await this.getUnrestrictedWithDocuments(
{ locale: language },
{ language },
null,
{
limit: 1,
Expand Down
20 changes: 18 additions & 2 deletions app/api/entities/specs/entities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1345,15 +1345,24 @@ describe('entities', () => {
});

describe('addLanguage()', () => {
it('should duplicate all the entities from the default language to the new one', async () => {
jest.spyOn(entities, 'createThumbnail').mockImplementation(entity => {
let createThumbnailSpy;

beforeAll(async () => {
createThumbnailSpy = jest.spyOn(entities, 'createThumbnail').mockImplementation(entity => {
if (!entity.file) {
return Promise.reject(
new Error('entities without file should not try to create thumbnail')
);
}
return Promise.resolve();
});
});

afterAll(() => {
createThumbnailSpy.mockRestore();
});

it('should duplicate all the entities from the default language to the new one', async () => {
await entities.saveMultiple([{ _id: docId1, file: {} }]);

await entities.addLanguage('ab', 2);
Expand All @@ -1364,6 +1373,13 @@ describe('entities', () => {
const toCheckPermissions = newEntities.find(e => e.title === 'Unpublished entity ES');
expect(toCheckPermissions.permissions).toEqual(fromCheckPermissions.permissions);
});

it('should not try to add already existing languages', async () => {
const oldCount = (await entities.get({ language: 'en' })).length;
await entities.addLanguage('en');
const newCount = (await entities.get({ language: 'en' })).length;
expect(newCount).toBe(oldCount);
});
});

describe('removeLanguage()', () => {
Expand Down
4 changes: 3 additions & 1 deletion app/api/i18n/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { UITranslationNotAvailable } from 'api/i18n/defaultTranslations';
import needsAuthorization from '../auth/authMiddleware';
import translations from './translations';

const addLanguage = async (language: any) => {
const addLanguage = async (language: LanguageSchema) => {
const newSettings = await settings.addLanguage(language);
const addedTranslations = await translations.addLanguage(language.key);
const newTranslations = addedTranslations
Expand Down Expand Up @@ -237,3 +237,5 @@ export default (app: Application) => {
}
);
};

export { addLanguage };
20 changes: 20 additions & 0 deletions app/api/i18n/specs/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ const fixtures: DBFixture = {
type: 'template',
},
],
entities: [
{
language: 'en',
sharedId: 'entity1',
title: '1',
template: entityTemplateId,
published: false,
metadata: {},
},
],
pages: [
{
title: 'testpage',
metadata: {
content: '',
},
language: 'en',
sharedId: 'aj07me9fino',
},
],
};

export default fixtures;
Expand Down
28 changes: 20 additions & 8 deletions app/api/i18n/specs/translations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import db from 'api/utils/testing_db';

import entities from 'api/entities';
import pages from 'api/pages';
import settings from 'api/settings';
import thesauri from 'api/thesauri/thesauri.js';
import { ContextType } from 'shared/translationSchema';
Expand All @@ -9,6 +11,7 @@ import { UITranslationNotAvailable } from '../defaultTranslations';
import translations from '../translations';
import fixtures, { dictionaryId } from './fixtures';
import { sortByLocale } from './sortByLocale';
import { addLanguage } from '../routes';

describe('translations', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -352,8 +355,7 @@ describe('translations', () => {

describe('addLanguage', () => {
it('should clone translations of default language and change language to the one added', async () => {
await settings.addLanguage({ key: 'fr', label: 'french' });
await translations.addLanguage('fr');
await addLanguage({ key: 'fr', label: 'french' });
const allTranslations = await translations.get();

const frTranslation = allTranslations.find(t => t.locale === 'fr');
Expand All @@ -362,16 +364,26 @@ describe('translations', () => {
expect(frTranslation?.contexts?.[0].values).toEqual(defaultTranslation.contexts?.[0].values);
});

describe('when translation already exists', () => {
describe('when the language already exists', () => {
it('should not clone it again', async () => {
await settings.addLanguage({ key: 'fr', label: 'french' });
await translations.addLanguage('fr');
await translations.addLanguage('fr');
const allTranslations = await translations.get();
await addLanguage({ key: 'fr', label: 'french' });

const frTranslations = allTranslations.filter(t => t.locale === 'fr');
const firstEntitiesCount = (await entities.get({ language: 'fr' })).length;
const firstPagesCount = (await pages.get({ language: 'fr' })).length;

await addLanguage({ key: 'fr', label: 'french' });

const settingsLanguages = (await settings.get()).languages?.map(l => l.key);
expect(settingsLanguages).toEqual(['es', 'en', 'zh', 'fr']);

const allTranslations = await translations.get();
const frTranslations = allTranslations.filter(t => t.locale === 'fr');
expect(frTranslations.length).toBe(1);

const secondEntitiesCount = (await entities.get({ language: 'fr' })).length;
const secondPagesCount = (await pages.get({ language: 'fr' })).length;
expect(firstEntitiesCount).toBe(secondEntitiesCount);
expect(firstPagesCount).toBe(secondPagesCount);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion app/api/pages/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default {
},

async addLanguage(language: string) {
const [lanuageTranslationAlreadyExists] = await this.get({ locale: language });
const [lanuageTranslationAlreadyExists] = await this.get({ language });
if (lanuageTranslationAlreadyExists) {
return Promise.resolve();
}
Expand Down
7 changes: 7 additions & 0 deletions app/api/pages/specs/pages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ describe('pages', () => {
const newPages = await pages.get({ language: 'ab' });
expect(newPages.length).toBe(3);
});

it('should not duplicate the pages if the language already exists', async () => {
const oldCount = await pages.get({ language: 'en' });
await pages.addLanguage('en');
const newCount = await pages.get({ language: 'en' });
expect(newCount.length).toBe(oldCount.length);
});
});

describe('getById', () => {
Expand Down
3 changes: 2 additions & 1 deletion app/api/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ export default {
async addLanguage(language: LanguageSchema) {
const currentSettings = await this.get();
currentSettings.languages = currentSettings.languages || [];
currentSettings.languages.push(language);
const keys = new Set(currentSettings.languages.map(l => l.key));
if (!keys.has(language.key)) currentSettings.languages.push(language);
return settingsModel.save(currentSettings);
},

Expand Down
10 changes: 9 additions & 1 deletion app/api/settings/specs/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,15 @@ describe('settings', () => {
it('should add a to settings list language', async () => {
await settings.addLanguage({ key: 'fr', label: 'Frances' });
const result = await settings.get();
expect(result.languages?.length).toBe(3);
const languages = result.languages?.map(l => l.key);
expect(languages).toEqual(['es', 'en', 'fr']);
});

it('should not add a language if it already exists', async () => {
await settings.addLanguage({ key: 'en', label: 'English' });
const result = await settings.get();
const languages = result.languages?.map(l => l.key);
expect(languages).toEqual(['es', 'en']);
});
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.144.0-rc1",
"version": "1.144.0-rc2",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit 00832c0

Please sign in to comment.