Merge branch 'next' into develop

This commit is contained in:
nocobase[bot] 2025-01-23 00:15:24 +00:00
commit c7c803fcff
3 changed files with 88 additions and 15 deletions

View File

@ -0,0 +1,66 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { Database } from '@nocobase/database';
import { MockServer, createMockServer } from '@nocobase/test';
describe('issues', async () => {
let app: MockServer;
let db: Database;
beforeEach(async () => {
app = await createMockServer({
plugins: ['collection-tree'],
});
db = app.db;
db.collection({
name: 'tree',
tree: 'adjacency-list',
fields: [
{
type: 'string',
name: 'name',
},
{
type: 'belongsTo',
name: 'parent',
foreignKey: 'parentId',
treeParent: true,
},
{
type: 'hasMany',
name: 'children',
foreignKey: 'parentId',
treeChildren: true,
},
],
});
await db.sync();
});
afterEach(async () => {
await db.clean({ drop: true });
await app.destroy();
});
it('should not set itself as parent', async () => {
expect.assertions(1);
const root = await db.getRepository('tree').create({
values: {
name: 'root',
},
});
try {
await root.update({
parentId: root.get('id'),
});
} catch (error) {
expect(error.message).toBe('Cannot set itself as the parent node');
}
});
});

View File

@ -278,21 +278,21 @@ describe('tree path test', () => {
for (const node of allNodes) {
expect(nodeA1.get(treeCollection.filterTargetKey) === node.get('rootPk')).toBeTruthy();
}
await treeCollection.repository.update({
values: {
parentId: nodeA4.get(treeCollection.filterTargetKey),
},
filter: {
name: 'a4',
},
});
const pathDataA4New = await db.getCollection(name).repository.findOne({
filter: {
[nodePkColumnName]: nodeA4.get(treeCollection.filterTargetKey),
},
});
// node primary key shoud be equal to root primary key to avoid infinite loop
expect(pathDataA4New.get('nodePk') === pathDataA4New.get('rootPk')).toBeTruthy();
// await treeCollection.repository.update({
// values: {
// parentId: nodeA4.get(treeCollection.filterTargetKey),
// },
// filter: {
// name: 'a4',
// },
// });
// const pathDataA4New = await db.getCollection(name).repository.findOne({
// filter: {
// [nodePkColumnName]: nodeA4.get(treeCollection.filterTargetKey),
// },
// });
// // node primary key shoud be equal to root primary key to avoid infinite loop
// expect(pathDataA4New.get('nodePk') === pathDataA4New.get('rootPk')).toBeTruthy();
});
it('test tree find one', async () => {

View File

@ -106,6 +106,13 @@ class PluginCollectionTreeServer extends Plugin {
transaction: options.transaction,
});
});
this.db.on(`${collection.name}.beforeSave`, async (model: Model) => {
const tk = collection.filterTargetKey as string;
if (model.get(parentForeignKey) === model.get(tk)) {
throw new Error('Cannot set itself as the parent node');
}
});
});
}
});