2023-06-21 17:00:00 +03:00
import assert from 'assert/strict' ;
2023-09-07 11:40:27 +03:00
import sinon from 'sinon' ;
2023-06-21 17:00:00 +03:00
import DomainEvents from '@tryghost/domain-events' ;
2023-06-20 13:03:36 +03:00
import {
CollectionsService ,
CollectionsRepositoryInMemory ,
2023-06-26 12:29:32 +03:00
PostAddedEvent ,
2023-07-27 16:04:14 +03:00
PostEditedEvent ,
TagDeletedEvent
2023-06-20 13:03:36 +03:00
} from '../src/index' ;
2023-07-31 10:58:21 +03:00
import {
2023-07-31 15:24:50 +03:00
PostsBulkUnpublishedEvent ,
PostsBulkFeaturedEvent ,
2023-08-23 11:39:20 +03:00
PostsBulkUnfeaturedEvent ,
PostsBulkAddTagsEvent
2023-07-31 10:58:21 +03:00
} from '@tryghost/post-events' ;
2023-06-01 14:16:24 +03:00
import { PostsRepositoryInMemory } from './fixtures/PostsRepositoryInMemory' ;
2023-07-27 16:03:14 +03:00
import { posts as postFixtures } from './fixtures/posts' ;
import { CollectionPost } from '../src/CollectionPost' ;
2023-05-25 18:34:59 +03:00
2023-07-31 15:24:50 +03:00
const initPostsRepository = async ( posts : any ) : Promise < PostsRepositoryInMemory > = > {
2023-06-01 14:16:24 +03:00
const postsRepository = new PostsRepositoryInMemory ( ) ;
for ( const post of posts ) {
const collectionPost = {
id : post.id ,
title : post.title ,
slug : post.slug ,
featured : post.featured ,
2023-07-31 10:58:21 +03:00
published_at : post.published_at?.toISOString ( ) ,
2023-07-27 16:03:14 +03:00
tags : post.tags ,
2023-06-01 14:16:24 +03:00
deleted : false
} ;
2023-07-31 15:24:50 +03:00
await postsRepository . save ( collectionPost as CollectionPost & { deleted : false } ) ;
2023-06-01 14:16:24 +03:00
}
return postsRepository ;
} ;
2023-05-25 18:34:59 +03:00
describe ( 'CollectionsService' , function ( ) {
2023-05-17 12:11:14 +03:00
let collectionsService : CollectionsService ;
2023-06-20 13:03:36 +03:00
let postsRepository : PostsRepositoryInMemory ;
2023-05-25 18:34:59 +03:00
beforeEach ( async function ( ) {
const collectionsRepository = new CollectionsRepositoryInMemory ( ) ;
2023-07-31 15:24:50 +03:00
postsRepository = await initPostsRepository ( postFixtures ) ;
2023-05-17 12:11:14 +03:00
2023-05-25 18:34:59 +03:00
collectionsService = new CollectionsService ( {
2023-06-01 14:16:24 +03:00
collectionsRepository ,
2023-06-26 07:01:43 +03:00
postsRepository ,
2023-06-29 00:50:50 +03:00
DomainEvents ,
slugService : {
async generate ( input ) {
return input . replace ( /\s+/g , '-' ) . toLowerCase ( ) ;
}
}
2023-05-25 18:34:59 +03:00
} ) ;
2023-05-17 12:11:14 +03:00
} ) ;
it ( 'Instantiates a CollectionsService' , function ( ) {
2023-05-16 07:02:58 +03:00
assert . ok ( collectionsService , 'CollectionsService should initialize' ) ;
} ) ;
it ( 'Can do CRUD operations on a collection' , async function ( ) {
2023-06-01 07:16:39 +03:00
const savedCollection = await collectionsService . createCollection ( {
2023-05-16 07:02:58 +03:00
title : 'testing collections' ,
description : 'testing collections description' ,
type : 'manual' ,
2023-06-01 07:16:39 +03:00
filter : null
2023-05-16 07:02:58 +03:00
} ) ;
2023-05-17 09:42:35 +03:00
const createdCollection = await collectionsService . getById ( savedCollection . id ) ;
2023-05-16 07:02:58 +03:00
assert . ok ( createdCollection , 'Collection should be saved' ) ;
2023-06-16 12:31:37 +03:00
assert . ok ( createdCollection . id , 'Collection should have an id' ) ;
2023-05-16 07:02:58 +03:00
assert . equal ( createdCollection . title , 'testing collections' , 'Collection title should match' ) ;
const allCollections = await collectionsService . getAll ( ) ;
2023-05-17 09:52:27 +03:00
assert . equal ( allCollections . data . length , 1 , 'There should be one collection' ) ;
2023-05-16 07:02:58 +03:00
2023-06-16 12:31:37 +03:00
await collectionsService . destroy ( createdCollection . id ) ;
2023-05-17 09:42:35 +03:00
const deletedCollection = await collectionsService . getById ( savedCollection . id ) ;
2023-05-16 07:02:58 +03:00
assert . equal ( deletedCollection , null , 'Collection should be deleted' ) ;
} ) ;
2023-05-17 09:42:35 +03:00
2023-06-16 12:31:37 +03:00
it ( 'Can retrieve a collection by slug' , async function ( ) {
const savedCollection = await collectionsService . createCollection ( {
title : 'slug test' ,
slug : 'get-me-by-slug' ,
type : 'manual' ,
filter : null
} ) ;
const retrievedCollection = await collectionsService . getBySlug ( 'get-me-by-slug' ) ;
assert . ok ( retrievedCollection , 'Collection should be saved' ) ;
assert . ok ( retrievedCollection . slug , 'Collection should have a slug' ) ;
assert . equal ( savedCollection . title , 'slug test' , 'Collection title should match' ) ;
const nonExistingCollection = await collectionsService . getBySlug ( 'i-do-not-exist' ) ;
assert . equal ( nonExistingCollection , null , 'Collection should not exist' ) ;
} ) ;
2023-06-06 18:04:02 +03:00
it ( 'Throws when built in collection is attempted to be deleted' , async function ( ) {
const collection = await collectionsService . createCollection ( {
title : 'Featured Posts' ,
slug : 'featured' ,
description : 'Collection of featured posts' ,
type : 'automatic' ,
deletable : false ,
filter : 'featured:true'
} ) ;
await assert . rejects ( async ( ) = > {
await collectionsService . destroy ( collection . id ) ;
} , ( err : any ) = > {
assert . equal ( err . message , 'Cannot delete builtin collection' , 'Error message should match' ) ;
assert . equal ( err . context , ` The collection ${ collection . id } is a builtin collection and cannot be deleted ` , 'Error context should match' ) ;
return true ;
} ) ;
} ) ;
2023-06-07 16:06:15 +03:00
describe ( 'getCollectionsForPost' , function ( ) {
it ( 'Can get collections for a post' , async function ( ) {
const collection = await collectionsService . createCollection ( {
title : 'testing collections' ,
2023-07-12 15:14:28 +03:00
slug : 'testing-collections' ,
type : 'manual'
} ) ;
const collection2 = await collectionsService . createCollection ( {
title : 'testing collections 1' ,
slug : '1-testing-collections' ,
2023-06-07 16:06:15 +03:00
type : 'manual'
} ) ;
2023-06-01 09:43:20 +03:00
2023-07-27 16:03:14 +03:00
await collectionsService . addPostToCollection ( collection . id , postFixtures [ 0 ] ) ;
await collectionsService . addPostToCollection ( collection2 . id , postFixtures [ 0 ] ) ;
2023-06-07 16:06:15 +03:00
2023-07-27 16:03:14 +03:00
const collections = await collectionsService . getCollectionsForPost ( postFixtures [ 0 ] . id ) ;
2023-06-07 16:06:15 +03:00
2023-07-12 15:14:28 +03:00
assert . equal ( collections . length , 2 , 'There should be one collection' ) ;
assert . equal ( collections [ 0 ] . id , collection2 . id , 'Collections should be sorted by slug' ) ;
assert . equal ( collections [ 1 ] . id , collection . id , 'Collections should be sorted by slug' ) ;
2023-06-01 09:43:20 +03:00
} ) ;
2023-06-13 09:50:27 +03:00
} ) ;
2023-06-01 07:16:39 +03:00
describe ( 'addPostToCollection' , function ( ) {
it ( 'Can add a Post to a Collection' , async function ( ) {
const collection = await collectionsService . createCollection ( {
title : 'testing collections' ,
description : 'testing collections description' ,
type : 'manual'
} ) ;
2023-07-27 16:03:14 +03:00
const editedCollection = await collectionsService . addPostToCollection ( collection . id , postFixtures [ 0 ] ) ;
2023-06-01 07:16:39 +03:00
assert . equal ( editedCollection ? . posts . length , 1 , 'Collection should have one post' ) ;
2023-07-27 16:03:14 +03:00
assert . equal ( editedCollection ? . posts [ 0 ] . id , postFixtures [ 0 ] . id , 'Collection should have the correct post' ) ;
2023-06-01 07:16:39 +03:00
} ) ;
it ( 'Does not error when trying to add a post to a collection that does not exist' , async function ( ) {
2023-07-27 16:03:14 +03:00
const editedCollection = await collectionsService . addPostToCollection ( 'fake id' , postFixtures [ 0 ] ) ;
2023-06-01 07:16:39 +03:00
assert ( editedCollection === null ) ;
} ) ;
} ) ;
2023-05-17 17:59:03 +03:00
describe ( 'edit' , function ( ) {
it ( 'Can edit existing collection' , async function ( ) {
2023-06-01 07:16:39 +03:00
const savedCollection = await collectionsService . createCollection ( {
2023-05-17 17:59:03 +03:00
title : 'testing collections' ,
description : 'testing collections description' ,
2023-06-01 07:16:39 +03:00
type : 'manual'
2023-05-17 17:59:03 +03:00
} ) ;
const editedCollection = await collectionsService . edit ( {
id : savedCollection.id ,
2023-06-01 08:37:51 +03:00
title : 'Edited title' ,
description : 'Edited description' ,
2023-06-29 00:50:50 +03:00
feature_image : '/assets/images/edited.jpg' ,
slug : 'changed'
2023-05-17 17:59:03 +03:00
} ) ;
2023-06-01 08:37:51 +03:00
assert . equal ( editedCollection ? . title , 'Edited title' , 'Collection title should be edited' ) ;
2023-06-29 00:50:50 +03:00
assert . equal ( editedCollection ? . slug , 'changed' , 'Collection slug should be edited' ) ;
2023-05-17 17:59:03 +03:00
assert . equal ( editedCollection ? . description , 'Edited description' , 'Collection description should be edited' ) ;
2023-06-01 08:37:51 +03:00
assert . equal ( editedCollection ? . feature_image , '/assets/images/edited.jpg' , 'Collection feature_image should be edited' ) ;
assert . equal ( editedCollection ? . type , 'manual' , 'Collection type should not be edited' ) ;
2023-05-17 17:59:03 +03:00
} ) ;
it ( 'Resolves to null when editing unexistend collection' , async function ( ) {
const editedCollection = await collectionsService . edit ( {
id : '12345'
} ) ;
assert . equal ( editedCollection , null , 'Collection should be null' ) ;
} ) ;
2023-05-25 18:34:59 +03:00
it ( 'Adds a Post to a Collection' , async function ( ) {
2023-06-01 07:16:39 +03:00
const collection = await collectionsService . createCollection ( {
2023-05-25 18:34:59 +03:00
title : 'testing collections' ,
description : 'testing collections description' ,
2023-06-01 07:16:39 +03:00
type : 'manual'
2023-05-25 18:34:59 +03:00
} ) ;
const editedCollection = await collectionsService . edit ( {
id : collection.id ,
posts : [ {
2023-07-27 16:03:14 +03:00
id : postFixtures [ 0 ] . id
2023-05-25 18:34:59 +03:00
} ]
} ) ;
assert . equal ( editedCollection ? . posts . length , 1 , 'Collection should have one post' ) ;
2023-07-27 16:03:14 +03:00
assert . equal ( editedCollection ? . posts [ 0 ] . id , postFixtures [ 0 ] . id , 'Collection should have the correct post' ) ;
2023-06-01 08:37:51 +03:00
assert . equal ( editedCollection ? . posts [ 0 ] . sort_order , 0 , 'Collection should have the correct post sort order' ) ;
2023-05-25 18:34:59 +03:00
} ) ;
2023-06-01 10:57:47 +03:00
it ( 'Removes a Post from a Collection' , async function ( ) {
const collection = await collectionsService . createCollection ( {
title : 'testing collections' ,
description : 'testing collections description' ,
type : 'manual'
} ) ;
let editedCollection = await collectionsService . edit ( {
id : collection.id ,
posts : [ {
2023-07-27 16:03:14 +03:00
id : postFixtures [ 0 ] . id
2023-06-01 10:57:47 +03:00
} , {
2023-07-27 16:03:14 +03:00
id : postFixtures [ 1 ] . id
2023-06-01 10:57:47 +03:00
} ]
} ) ;
assert . equal ( editedCollection ? . posts . length , 2 , 'Collection should have two posts' ) ;
2023-07-27 16:03:14 +03:00
editedCollection = await collectionsService . removePostFromCollection ( collection . id , postFixtures [ 0 ] . id ) ;
2023-06-01 10:57:47 +03:00
assert . equal ( editedCollection ? . posts . length , 1 , 'Collection should have one posts' ) ;
} ) ;
it ( 'Returns null when removing post from non existing collection' , async function ( ) {
2023-07-27 16:03:14 +03:00
const collection = await collectionsService . removePostFromCollection ( 'i-do-not-exist' , postFixtures [ 0 ] . id ) ;
2023-06-01 10:57:47 +03:00
assert . equal ( collection , null , 'Collection should be null' ) ;
} ) ;
2023-05-17 17:59:03 +03:00
} ) ;
2023-06-01 14:16:24 +03:00
describe ( 'Automatic Collections' , function ( ) {
it ( 'Can create an automatic collection' , async function ( ) {
const collection = await collectionsService . createCollection ( {
title : 'I am automatic' ,
description : 'testing automatic collection' ,
type : 'automatic' ,
filter : 'featured:true'
} ) ;
assert . equal ( collection . type , 'automatic' , 'Collection should be automatic' ) ;
assert . equal ( collection . filter , 'featured:true' , 'Collection should have the correct filter' ) ;
2023-06-05 12:22:51 +03:00
assert . equal ( collection . posts . length , 2 , 'Collection should have two posts' ) ;
2023-06-01 14:16:24 +03:00
} ) ;
2023-06-05 12:22:51 +03:00
it ( 'Updates the automatic collection posts when the filter is changed' , async function ( ) {
let collection = await collectionsService . createCollection ( {
title : 'I am automatic' ,
description : 'testing automatic collection' ,
type : 'automatic' ,
filter : 'featured:true'
} ) ;
assert . equal ( collection ? . type , 'automatic' , 'Collection should be automatic' ) ;
assert . equal ( collection ? . posts . length , 2 , 'Collection should have two featured post' ) ;
assert . equal ( collection ? . posts [ 0 ] . id , 'post-3-featured' , 'Collection should have the correct post' ) ;
assert . equal ( collection ? . posts [ 1 ] . id , 'post-4-featured' , 'Collection should have the correct post' ) ;
let updatedCollection = await collectionsService . edit ( {
id : collection.id ,
2023-08-17 11:46:41 +03:00
filter : 'featured:true+published_at:>2023-05-20'
2023-06-05 12:22:51 +03:00
} ) ;
assert . equal ( updatedCollection ? . posts . length , 1 , 'Collection should have one post' ) ;
2023-07-27 16:03:14 +03:00
assert . equal ( updatedCollection ? . posts [ 0 ] . id , 'post-3-featured' , 'Collection should have the correct post' ) ;
2023-06-05 12:22:51 +03:00
} ) ;
2023-06-05 14:33:04 +03:00
2023-06-20 13:03:36 +03:00
describe ( 'updateCollections' , function ( ) {
let automaticFeaturedCollection : any ;
let automaticNonFeaturedCollection : any ;
let manualCollection : any ;
beforeEach ( async function ( ) {
automaticFeaturedCollection = await collectionsService . createCollection ( {
title : 'Featured Collection' ,
description : 'testing automatic collection' ,
type : 'automatic' ,
filter : 'featured:true'
} ) ;
automaticNonFeaturedCollection = await collectionsService . createCollection ( {
title : 'Non-Featured Collection' ,
description : 'testing automatic collection' ,
type : 'automatic' ,
filter : 'featured:false'
} ) ;
manualCollection = await collectionsService . createCollection ( {
title : 'Manual Collection' ,
description : 'testing manual collection' ,
type : 'manual'
} ) ;
2023-07-27 16:03:14 +03:00
await collectionsService . addPostToCollection ( manualCollection . id , postFixtures [ 0 ] ) ;
await collectionsService . addPostToCollection ( manualCollection . id , postFixtures [ 1 ] ) ;
2023-06-05 14:33:04 +03:00
} ) ;
2023-06-20 13:03:36 +03:00
afterEach ( async function ( ) {
await collectionsService . destroy ( automaticFeaturedCollection . id ) ;
await collectionsService . destroy ( automaticNonFeaturedCollection . id ) ;
await collectionsService . destroy ( manualCollection . id ) ;
2023-06-05 14:33:04 +03:00
} ) ;
2023-07-27 16:04:14 +03:00
it ( 'Updates all automatic collections when a tag is deleted' , async function ( ) {
const collectionsRepository = new CollectionsRepositoryInMemory ( ) ;
2023-07-31 15:24:50 +03:00
postsRepository = await initPostsRepository ( [
2023-07-27 16:04:14 +03:00
{
id : 'post-1' ,
url : 'http://localhost:2368/post-1/' ,
title : 'Post 1' ,
slug : 'post-1' ,
featured : false ,
tags : [ { slug : 'to-be-deleted' } , { slug : 'other-tag' } ] ,
created_at : new Date ( '2023-03-15T07:19:07.447Z' ) ,
updated_at : new Date ( '2023-03-15T07:19:07.447Z' ) ,
published_at : new Date ( '2023-03-15T07:19:07.447Z' )
} , {
id : 'post-2' ,
url : 'http://localhost:2368/post-2/' ,
title : 'Post 2' ,
slug : 'post-2' ,
featured : false ,
tags : [ { slug : 'to-be-deleted' } , { slug : 'other-tag' } ] ,
created_at : new Date ( '2023-04-05T07:20:07.447Z' ) ,
updated_at : new Date ( '2023-04-05T07:20:07.447Z' ) ,
published_at : new Date ( '2023-04-05T07:20:07.447Z' )
}
] ) ;
collectionsService = new CollectionsService ( {
collectionsRepository ,
postsRepository ,
DomainEvents ,
slugService : {
async generate ( input ) {
return input . replace ( /\s+/g , '-' ) . toLowerCase ( ) ;
}
}
} ) ;
const automaticCollectionWithTag = await collectionsService . createCollection ( {
title : 'Automatic Collection with Tag' ,
description : 'testing automatic collection with tag' ,
type : 'automatic' ,
filter : 'tags:to-be-deleted'
} ) ;
const automaticCollectionWithoutTag = await collectionsService . createCollection ( {
title : 'Automatic Collection without Tag' ,
description : 'testing automatic collection without tag' ,
type : 'automatic' ,
filter : 'tags:other-tag'
} ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithTag . id ) ) ? . posts . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithoutTag . id ) ) ? . posts . length , 2 ) ;
collectionsService . subscribeToEvents ( ) ;
const tagDeletedEvent = TagDeletedEvent . create ( {
id : 'to-be-deleted'
} ) ;
const posts = await postsRepository . getAll ( ) ;
for ( const post of posts ) {
post . tags = post . tags . filter ( tag = > tag . slug !== 'to-be-deleted' ) ;
await postsRepository . save ( post ) ;
}
DomainEvents . dispatch ( tagDeletedEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithTag . id ) ) ? . posts . length , 0 ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithoutTag . id ) ) ? . posts . length , 2 ) ;
} ) ;
2023-08-23 11:39:20 +03:00
it ( 'Updates all collections when post tags are added in bulk' , async function ( ) {
const collectionsRepository = new CollectionsRepositoryInMemory ( ) ;
postsRepository = await initPostsRepository ( [
{
id : 'post-1' ,
url : 'http://localhost:2368/post-1/' ,
title : 'Post 1' ,
slug : 'post-1' ,
featured : false ,
tags : [ { slug : 'existing-tag' } ] ,
created_at : new Date ( '2023-03-15T07:19:07.447Z' ) ,
updated_at : new Date ( '2023-03-15T07:19:07.447Z' ) ,
published_at : new Date ( '2023-03-15T07:19:07.447Z' )
} , {
id : 'post-2' ,
url : 'http://localhost:2368/post-2/' ,
title : 'Post 2' ,
slug : 'post-2' ,
featured : false ,
tags : [ ] ,
created_at : new Date ( '2023-04-05T07:20:07.447Z' ) ,
updated_at : new Date ( '2023-04-05T07:20:07.447Z' ) ,
published_at : new Date ( '2023-04-05T07:20:07.447Z' )
}
] ) ;
collectionsService = new CollectionsService ( {
collectionsRepository ,
postsRepository ,
DomainEvents ,
slugService : {
async generate ( input ) {
return input . replace ( /\s+/g , '-' ) . toLowerCase ( ) ;
}
}
} ) ;
const automaticCollectionWithExistingTag = await collectionsService . createCollection ( {
title : 'Automatic Collection with Tag' ,
description : 'testing automatic collection with tag' ,
type : 'automatic' ,
filter : 'tags:existing-tag'
} ) ;
const automaticCollectionWithBulkAddedTag = await collectionsService . createCollection ( {
title : 'Automatic Collection without Tag' ,
description : 'testing automatic collection without tag' ,
type : 'automatic' ,
filter : 'tags:to-be-created'
} ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithExistingTag . id ) ) ? . posts . length , 1 ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithBulkAddedTag . id ) ) ? . posts . length , 0 ) ;
collectionsService . subscribeToEvents ( ) ;
const posts = await postsRepository . getAll ( ) ;
for ( const post of posts ) {
post . tags . push ( { slug : 'to-be-created' } ) ;
await postsRepository . save ( post ) ;
}
const postsBulkAddTagsEvent = PostsBulkAddTagsEvent . create ( [
'post-1' ,
'post-2'
] ) ;
DomainEvents . dispatch ( postsBulkAddTagsEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithExistingTag . id ) ) ? . posts . length , 1 ) ;
assert . equal ( ( await collectionsService . getById ( automaticCollectionWithBulkAddedTag . id ) ) ? . posts . length , 2 ) ;
} ) ;
2023-06-20 13:03:36 +03:00
it ( 'Updates all collections when post is deleted' , async function ( ) {
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
2023-09-15 08:08:38 +03:00
await collectionsService . removePostFromAllCollections ( postFixtures [ 0 ] . id ) ;
2023-06-05 14:33:04 +03:00
2023-06-20 13:03:36 +03:00
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 1 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 1 ) ;
} ) ;
2023-07-25 15:22:32 +03:00
it ( 'Updates all collections when posts are deleted in bulk' , async function ( ) {
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
2023-09-15 08:08:38 +03:00
const deletedPostIds = [
2023-07-31 10:58:21 +03:00
postFixtures [ 0 ] . id ,
postFixtures [ 1 ] . id
2023-09-15 08:08:38 +03:00
] ;
await collectionsService . removePostsFromAllCollections ( deletedPostIds ) ;
2023-07-25 15:22:32 +03:00
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 0 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 0 ) ;
} ) ;
2023-07-31 10:58:21 +03:00
it ( 'Updates collections with publish filter when PostsBulkUnpublishedEvent event is produced' , async function ( ) {
const publishedPostsCollection = await collectionsService . createCollection ( {
title : 'Published Posts' ,
slug : 'published-posts' ,
type : 'automatic' ,
filter : 'published_at:>=2023-05-00T00:00:00.000Z'
} ) ;
assert . equal ( ( await collectionsService . getById ( publishedPostsCollection . id ) ) ? . posts . length , 2 , 'Only two post fixtures are published on the 5th month of 2023' ) ;
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
collectionsService . subscribeToEvents ( ) ;
2023-07-31 15:24:50 +03:00
await postsRepository . save ( Object . assign ( postFixtures [ 2 ] , {
2023-07-31 10:58:21 +03:00
published_at : null
} ) ) ;
const postsBulkUnpublishedEvent = PostsBulkUnpublishedEvent . create ( [
postFixtures [ 2 ] . id
] ) ;
DomainEvents . dispatch ( postsBulkUnpublishedEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( publishedPostsCollection . id ) ) ? . posts . length , 1 , 'Only one post left as published on the 5th month of 2023' ) ;
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts . length , 2 , 'There should be no change to the featured filter collection' ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 2 , 'There should be no change to the non-featured filter collection' ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 , 'There should be no change to the manual collection' ) ;
} ) ;
2023-07-31 15:24:50 +03:00
it ( 'Updates collections with publish filter when PostsBulkFeaturedEvent/PostsBulkUnfeaturedEvent events are produced' , async function ( ) {
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
collectionsService . subscribeToEvents ( ) ;
const featuredPost = await postsRepository . getById ( postFixtures [ 0 ] . id ) ;
if ( featuredPost ) {
featuredPost . featured = true ;
}
await postsRepository . save ( featuredPost as CollectionPost & { deleted : false } ) ;
const postsBulkFeaturedEvent = PostsBulkFeaturedEvent . create ( [
postFixtures [ 0 ] . id
] ) ;
DomainEvents . dispatch ( postsBulkFeaturedEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts . length , 3 , 'There should be one extra post in the featured filter collection' ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 1 , 'There should be one less posts in the non-featured filter collection' ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 , 'There should be no change to the manual collection' ) ;
const unFeaturedPost2 = await postsRepository . getById ( postFixtures [ 2 ] . id ) ;
if ( unFeaturedPost2 ) {
unFeaturedPost2 . featured = false ;
}
await postsRepository . save ( unFeaturedPost2 as CollectionPost & { deleted : false } ) ;
const unFeaturedPost3 = await postsRepository . getById ( postFixtures [ 3 ] . id ) ;
if ( unFeaturedPost3 ) {
unFeaturedPost3 . featured = false ;
}
await postsRepository . save ( unFeaturedPost3 as CollectionPost & { deleted : false } ) ;
const postsBulkUnfeaturedEvent = PostsBulkUnfeaturedEvent . create ( [
postFixtures [ 2 ] . id ,
postFixtures [ 3 ] . id
] ) ;
DomainEvents . dispatch ( postsBulkUnfeaturedEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts . length , 1 , 'There should be two less posts in the featured filter collection' ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 3 , 'There should be two extra posts in the non-featured filter collection' ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 , 'There should be no change to the manual collection' ) ;
} ) ;
2023-06-23 09:56:06 +03:00
it ( 'Updates only index collection when a non-featured post is added' , async function ( ) {
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
collectionsService . subscribeToEvents ( ) ;
const postAddedEvent = PostAddedEvent . create ( {
id : 'non-featured-post' ,
featured : false
} ) ;
DomainEvents . dispatch ( postAddedEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 3 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
} ) ;
2023-07-31 10:58:21 +03:00
it ( 'Moves post from featured to non featured collection when the featured attribute is changed' , async function ( ) {
2023-06-26 12:29:32 +03:00
collectionsService . subscribeToEvents ( ) ;
2023-07-27 16:03:14 +03:00
const newFeaturedPost : CollectionPost & { deleted : false } = {
2023-06-26 12:29:32 +03:00
id : 'post-featured' ,
featured : false ,
published_at : new Date ( '2023-03-16T07:19:07.447Z' ) ,
2023-07-27 16:03:14 +03:00
tags : [ ] ,
2023-06-26 12:29:32 +03:00
deleted : false
} ;
await postsRepository . save ( newFeaturedPost ) ;
const updateCollectionEvent = PostEditedEvent . create ( {
id : newFeaturedPost.id ,
current : {
id : newFeaturedPost.id ,
featured : false
} ,
previous : {
id : newFeaturedPost.id ,
featured : true
}
} ) ;
DomainEvents . dispatch ( updateCollectionEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 3 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
// change featured back to true
const updateCollectionEventBackToFeatured = PostEditedEvent . create ( {
id : newFeaturedPost.id ,
current : {
id : newFeaturedPost.id ,
featured : true
} ,
previous : {
id : newFeaturedPost.id ,
featured : false
}
} ) ;
DomainEvents . dispatch ( updateCollectionEventBackToFeatured ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( ( await collectionsService . getById ( automaticFeaturedCollection . id ) ) ? . posts ? . length , 3 ) ;
assert . equal ( ( await collectionsService . getById ( automaticNonFeaturedCollection . id ) ) ? . posts . length , 2 ) ;
assert . equal ( ( await collectionsService . getById ( manualCollection . id ) ) ? . posts . length , 2 ) ;
} ) ;
2023-09-07 11:40:27 +03:00
it ( 'Does nothing when the PostEditedEvent contains no relevant changes' , async function ( ) {
collectionsService . subscribeToEvents ( ) ;
const updatePostInMatchingCollectionsSpy = sinon . spy ( collectionsService , 'updatePostInMatchingCollections' ) ;
const postEditEvent = PostEditedEvent . create ( {
id : 'something' ,
current : {
id : 'unique-post-id' ,
status : 'scheduled' ,
featured : true ,
tags : [ 'they' , 'do' , 'not' , 'change' ]
} ,
previous : {
id : 'unique-post-id' ,
status : 'published' ,
featured : true ,
tags : [ 'they' , 'do' , 'not' , 'change' ]
}
} ) ;
DomainEvents . dispatch ( postEditEvent ) ;
await DomainEvents . allSettled ( ) ;
assert . equal ( updatePostInMatchingCollectionsSpy . callCount , 0 , 'updatePostInMatchingCollections method should not have been called' ) ;
} ) ;
2023-06-05 14:33:04 +03:00
} ) ;
2023-06-01 14:16:24 +03:00
} ) ;
2023-05-16 07:02:58 +03:00
} ) ;