| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const { createTestBuilder } = require('../../../test/helpers/builder'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('../../../test/helpers/strapi'); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | const { createAuthRequest } = require('../../../test/helpers/request'); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const modelsUtils = require('../../../test/helpers/models'); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | let builder; | 
					
						
							|  |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | let rq; | 
					
						
							|  |  |  | let data = { | 
					
						
							|  |  |  |   dogs: [], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | const dogModel = { | 
					
						
							|  |  |  |   draftAndPublish: false, | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |     code: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |       unique: true, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  |   connection: 'default', | 
					
						
							|  |  |  |   name: 'dog', | 
					
						
							|  |  |  |   description: '', | 
					
						
							|  |  |  |   collectionName: '', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const dogs = [ | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     name: 'Nelson', | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |     code: '1', | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     name: 'Atos', | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |     code: '2', | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const restart = async () => { | 
					
						
							|  |  |  |   await strapi.destroy(); | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |   strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   rq = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  | const sortDogs = dogs => _.sortBy(dogs, 'name'); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('Migration - draft and publish', () => { | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |   describe.each([ | 
					
						
							|  |  |  |     ['without table modifications', {}, {}], | 
					
						
							|  |  |  |     ['with table modifications', { town: { type: 'string' } }, { color: { type: 'string' } }], | 
					
						
							|  |  |  |   ])('%p', (testName, tableModification1, tableModification2) => { | 
					
						
							|  |  |  |     beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  |       builder = createTestBuilder(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await builder | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |         .addContentType(dogModel) | 
					
						
							|  |  |  |         .addFixtures(dogModel.name, dogs) | 
					
						
							|  |  |  |         .build(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |       strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       rq = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       data.dogs = sortDogs(builder.sanitizedFixturesFor(dogModel.name, strapi)); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |     afterAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       await strapi.destroy(); | 
					
						
							|  |  |  |       await builder.cleanup(); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |     describe('Enabling D&P on a content-type', () => { | 
					
						
							|  |  |  |       test('No published_at before enabling the feature', async () => { | 
					
						
							|  |  |  |         let { body } = await rq({ | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |           url: '/content-manager/collection-types/application::dog.dog', | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |           method: 'GET', | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |         expect(body.results.length).toBe(2); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |         const sortedBody = sortDogs(body.results); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         sortedBody.forEach((dog, index) => { | 
					
						
							|  |  |  |           expect(dog).toMatchObject(data.dogs[index]); | 
					
						
							|  |  |  |           expect(dog.published_at).toBeUndefined(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |       test('Published_at is equal to created_at after enabling the feature', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |         const schema = await modelsUtils.getContentTypeSchema(dogModel.name, { strapi }); | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |         await modelsUtils.modifyContentType( | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ...schema, | 
					
						
							|  |  |  |             attributes: _.merge(schema.attributes, tableModification1), | 
					
						
							|  |  |  |             draftAndPublish: true, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { strapi } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await restart(); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         let { body } = await rq({ | 
					
						
							|  |  |  |           method: 'GET', | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |           url: '/content-manager/collection-types/application::dog.dog', | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |         expect(body.results.length).toBe(2); | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |         const sortedBody = sortDogs(body.results); | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         sortedBody.forEach((dog, index) => { | 
					
						
							|  |  |  |           expect(dog).toMatchObject(data.dogs[index]); | 
					
						
							|  |  |  |           expect(dog.published_at).toBe(dog.createdAt || dog.created_at); | 
					
						
							|  |  |  |           expect(!isNaN(new Date(dog.published_at).valueOf())).toBe(true); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         data.dogs = sortedBody; | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |     describe('Disabling D&P on a content-type', () => { | 
					
						
							|  |  |  |       test('No published_at after disabling the feature + draft removed', async () => { | 
					
						
							|  |  |  |         const res = await rq({ | 
					
						
							|  |  |  |           method: 'POST', | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |           url: `/content-manager/collection-types/application::dog.dog/${data.dogs[1].id}/actions/unpublish`, | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         data.dogs[1] = res.body; | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |         const schema = await modelsUtils.getContentTypeSchema(dogModel.name, { strapi }); | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |         await modelsUtils.modifyContentType( | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ...schema, | 
					
						
							|  |  |  |             draftAndPublish: false, | 
					
						
							|  |  |  |             attributes: _.merge(schema.attributes, tableModification2), | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { strapi } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await restart(); | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         // drafts should have been deleted with the migration, so we remove them
 | 
					
						
							|  |  |  |         data.dogs = data.dogs.filter(dog => !_.isNil(dog.published_at)); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |         let { body } = await rq({ | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |           url: '/content-manager/collection-types/application::dog.dog', | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |           method: 'GET', | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  |         expect(body.results.length).toBe(1); | 
					
						
							|  |  |  |         expect(body.results[0]).toMatchObject(_.pick(data.dogs[0], ['name'])); | 
					
						
							|  |  |  |         expect(body.results[0].published_at).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-11-20 18:21:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |       test('Unique constraint is kept after disabling the feature', async () => { | 
					
						
							|  |  |  |         const dogToCreate = { code: 'sameCode' }; | 
					
						
							| 
									
										
										
										
											2020-11-20 18:21:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |         let res = await rq({ | 
					
						
							|  |  |  |           method: 'POST', | 
					
						
							| 
									
										
										
										
											2020-12-18 12:59:07 +01:00
										 |  |  |           url: `/content-manager/collection-types/application::dog.dog/`, | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |           body: dogToCreate, | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-11-20 18:21:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |         expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |         expect(res.body).toMatchObject(dogToCreate); | 
					
						
							| 
									
										
										
										
											2020-11-20 18:21:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |         data.dogs.push(res.body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res = await rq({ | 
					
						
							|  |  |  |           method: 'POST', | 
					
						
							| 
									
										
										
										
											2020-12-18 12:59:07 +01:00
										 |  |  |           url: `/content-manager/collection-types/application::dog.dog/`, | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |           body: dogToCreate, | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-11-20 18:21:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-17 12:33:00 +01:00
										 |  |  |         expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |