diff --git a/src/m365/spo/commands/file/file-get.spec.ts b/src/m365/spo/commands/file/file-get.spec.ts index d8263e1f406..4f67c79c43e 100644 --- a/src/m365/spo/commands/file/file-get.spec.ts +++ b/src/m365/spo/commands/file/file-get.spec.ts @@ -200,6 +200,82 @@ describe(commands.FILE_GET, () => { })); }); + it('retrieves and prints all details of file as ListItem object with url', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if ((opts.url as string).indexOf('?$expand=ListItemAllFields') > -1) { + return { + "ListItemAllFields": { + "FileSystemObjectType": 0, + "Id": 4, + "ServerRedirectedEmbedUri": "https://contoso.sharepoint.com/sites/project-x/_layouts/15/WopiFrame.aspx?sourcedoc={b2307a39-e878-458b-bc90-03bc578531d6}&action=interactivepreview", + "ServerRedirectedEmbedUrl": "https://contoso.sharepoint.com/sites/project-x/_layouts/15/WopiFrame.aspx?sourcedoc={b2307a39-e878-458b-bc90-03bc578531d6}&action=interactivepreview", + "ContentTypeId": "0x0101008E462E3ACE8DB844B3BEBF9473311889", + "ComplianceAssetId": null, + "Title": null, + "ID": 4, + "Created": "2018-02-05T09:42:36", + "AuthorId": 1, + "Modified": "2018-02-05T09:44:03", + "EditorId": 1, + "OData__CopySource": null, + "CheckoutUserId": null, + "OData__UIVersionString": "3.0", + "GUID": "2054f49e-0f76-46d4-ac55-50e1c057941c" + }, + "CheckInComment": "", + "CheckOutType": 2, + "ContentTag": "{F09C4EFE-B8C0-4E89-A166-03418661B89B},9,12", + "CustomizedPageStatus": 0, + "ETag": "\"{F09C4EFE-B8C0-4E89-A166-03418661B89B},9\"", + "Exists": true, + "IrmEnabled": false, + "Length": "331673", + "Level": 1, + "LinkingUri": "https://contoso.sharepoint.com/sites/project-x/Documents/Test1.docx?d=wf09c4efeb8c04e89a16603418661b89b", + "LinkingUrl": "https://contoso.sharepoint.com/sites/project-x/Documents/Test1.docx?d=wf09c4efeb8c04e89a16603418661b89b", + "MajorVersion": 3, + "MinorVersion": 0, + "Name": "Opendag maart 2018.docx", + "ServerRelativeUrl": "/sites/project-x/Documents/Test1.docx", + "TimeCreated": "2018-02-05T08:42:36Z", + "TimeLastModified": "2018-02-05T08:44:03Z", + "Title": "", + "UIVersion": 1536, + "UIVersionLabel": "3.0", + "UniqueId": "b2307a39-e878-458b-bc90-03bc578531d6" + }; + } + + throw 'Invalid request'; + }); + + await command.action(logger, { + options: { + debug: true, + url: '/sites/project-x/Documents/Test1.docx', + webUrl: 'https://contoso.sharepoint.com/sites/project-x', + asListItem: true + } + }); + assert(loggerLogSpy.calledOnceWithExactly({ + "FileSystemObjectType": 0, + "Id": 4, + "ServerRedirectedEmbedUri": "https://contoso.sharepoint.com/sites/project-x/_layouts/15/WopiFrame.aspx?sourcedoc={b2307a39-e878-458b-bc90-03bc578531d6}&action=interactivepreview", + "ServerRedirectedEmbedUrl": "https://contoso.sharepoint.com/sites/project-x/_layouts/15/WopiFrame.aspx?sourcedoc={b2307a39-e878-458b-bc90-03bc578531d6}&action=interactivepreview", + "ContentTypeId": "0x0101008E462E3ACE8DB844B3BEBF9473311889", + "ComplianceAssetId": null, + "Title": null, + "Created": "2018-02-05T09:42:36", + "AuthorId": 1, + "Modified": "2018-02-05T09:44:03", + "EditorId": 1, + "OData__CopySource": null, + "CheckoutUserId": null, + "OData__UIVersionString": "3.0", + "GUID": "2054f49e-0f76-46d4-ac55-50e1c057941c" + })); + }); + it('retrieves and prints all details of file as ListItem object with permissions', async () => { sinon.stub(request, 'get').callsFake(async (opts) => { if ((opts.url as string).indexOf('?$expand=ListItemAllFields') > -1) { @@ -423,6 +499,8 @@ describe(commands.FILE_GET, () => { assert.strictEqual(getStub.lastCall.args[0].url, `https://contoso.sharepoint.com/sites/project-x/_api/web/GetFileByServerRelativePath(DecodedUrl=@f)?@f='%2Fsites%2Fproject-x%2FDocuments%2FTest1.docx'`); }); + + it('uses correct API url when tenant root URL option is passed', async () => { const getStub: any = sinon.stub(request, 'get').callsFake(async (opts) => { if ((opts.url as string).indexOf('/_api/web/GetFileByServerRelativePath(') > -1) { diff --git a/src/m365/spo/commands/page/page-add.spec.ts b/src/m365/spo/commands/page/page-add.spec.ts index 45a4ccfc845..8ac0d78ee22 100644 --- a/src/m365/spo/commands/page/page-add.spec.ts +++ b/src/m365/spo/commands/page/page-add.spec.ts @@ -12,8 +12,6 @@ import { session } from '../../../../utils/session.js'; import { sinonUtil } from '../../../../utils/sinonUtil.js'; import { spo } from '../../../../utils/spo.js'; import commands from '../../commands.js'; -import spoFileGetCommand from '../file/file-get.js'; -import spoListItemSetCommand from '../listitem/listitem-set.js'; import command from './page-add.js'; describe(commands.PAGE_ADD, () => { @@ -21,6 +19,19 @@ describe(commands.PAGE_ADD, () => { let logger: Logger; let loggerLogSpy: sinon.SinonSpy; let commandInfo: CommandInfo; + const fileResponse = + { + Attachments: false, + AuthorId: 3, + ContentTypeId: '0x0100B21BD271A810EE488B570BE49963EA34', + Created: '2018-03-15T10:43:10Z', + EditorId: 3, + GUID: '9a94cb88-019b-4a66-abd6-be7f5337f659', + ID: 6, + Id: 6, + Modified: '2018-03-15T10:52:10Z', + Title: 'NewTitle' + }; before(() => { sinon.stub(auth, 'restoreAuth').resolves(); @@ -56,8 +67,8 @@ describe(commands.PAGE_ADD, () => { afterEach(() => { sinonUtil.restore([ request.post, - Cli.executeCommand, - Cli.executeCommandWithOutput + spo.setListItem, + spo.getFileAsListItemByUrl ]); }); @@ -128,18 +139,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await assert.rejects(command.action(logger, { options: { name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a' } })); assert(loggerLogSpy.notCalled); @@ -215,18 +216,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: true, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a' } }); }); @@ -300,18 +291,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: true, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com' } }); }); @@ -496,18 +477,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: false, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a', layoutType: 'Home' } }); assert(loggerLogSpy.notCalled); }); @@ -588,18 +559,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: false, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a', promoteAs: 'NewsPage' } }); assert(loggerLogSpy.notCalled); }); @@ -687,18 +648,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: false, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a', promoteAs: 'Template', layoutType: 'Article' } } as any); assert(loggerLogSpy.notCalled); }); @@ -773,18 +724,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: true, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a', layoutType: 'Home', promoteAs: 'HomePage' } }); }); @@ -923,18 +864,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: false, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a', publish: false } }); assert.deepStrictEqual(savedAsDraft, true); }); @@ -1013,18 +944,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: false, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a', publish: true } }); assert(loggerLogSpy.notCalled); }); @@ -1103,18 +1024,8 @@ describe(commands.PAGE_ADD, () => { throw 'Invalid request'; }); - sinon.stub(Cli, 'executeCommand').callsFake(async (command): Promise => { - if (command === spoListItemSetCommand) { - return; - } - throw 'Invalid request'; - }); - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise => { - if (command === spoFileGetCommand) { - return { 'stdout': '{\"FileSystemObjectType\":0,\"Id\":6,\"ServerRedirectedEmbedUri\":null,\"ServerRedirectedEmbedUrl\":\"\",\"ContentTypeId\":\"0x0101009D1CB255DA76424F860D91F20E6C411800E2DAFA6353688E488147257C551A63BD\",\"ComplianceAssetId\":null,\"WikiField\":null,\"Title\":\"zzzz\",\"CanvasContent1\":\"
<\/div><\/div>\",\"BannerImageUrl\":{\"Description\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\",\"Url\":\"https:\/\/contoso.sharepoint.com\/_layouts\/15\/images\/sitepagethumbnail.png\"},\"Description\":null,\"PromotedState\":0,\"FirstPublishedDate\":\"2022-11-11T15:48:15\",\"LayoutWebpartsContent\":\"
<\/div><\/div>\",\"OData__AuthorBylineId\":[9],\"_AuthorBylineStringId\":[\"9\"],\"OData__TopicHeader\":null,\"OData__SPSitePageFlags\":null,\"OData__SPCallToAction\":null,\"OData__OriginalSourceUrl\":null,\"OData__OriginalSourceSiteId\":null,\"OData__OriginalSourceWebId\":null,\"OData__OriginalSourceListId\":null,\"OData__OriginalSourceItemId\":null,\"ID\":6,\"Created\":\"2022-11-11T15:48:00\",\"AuthorId\":9,\"Modified\":\"2022-11-12T02:03:12\",\"EditorId\":9,\"OData__CopySource\":null,\"CheckoutUserId\":9,\"OData__UIVersionString\":\"2.19\",\"GUID\":\"9a94cb88-019b-4a66-abd6-be7f5337f659\"}' }; - } - throw 'Invalid request'; - }); + sinon.stub(spo, 'setListItem').resolves(); + sinon.stub(spo, 'getFileAsListItemByUrl').resolves(fileResponse); await command.action(logger, { options: { debug: true, name: 'page.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a', publish: true, publishMessage: 'Initial version' } }); }); diff --git a/src/m365/spo/commands/page/page-add.ts b/src/m365/spo/commands/page/page-add.ts index a147ccc180f..d38605d0423 100644 --- a/src/m365/spo/commands/page/page-add.ts +++ b/src/m365/spo/commands/page/page-add.ts @@ -1,7 +1,5 @@ import { Auth } from '../../../../Auth.js'; -import { Cli, CommandOutput } from '../../../../cli/Cli.js'; import { Logger } from '../../../../cli/Logger.js'; -import Command from '../../../../Command.js'; import GlobalOptions from '../../../../GlobalOptions.js'; import request, { CliRequestOptions } from '../../../../request.js'; import { formatting } from '../../../../utils/formatting.js'; @@ -10,8 +8,6 @@ import { urlUtil } from '../../../../utils/urlUtil.js'; import { validation } from '../../../../utils/validation.js'; import SpoCommand from '../../../base/SpoCommand.js'; import commands from '../../commands.js'; -import spoFileGetCommand, { Options as spoFileGetOptions } from '../file/file-get.js'; -import spoListItemSetCommand, { Options as spoListItemSetOptions } from '../listitem/listitem-set.js'; import { Page, supportedPageLayouts, supportedPromoteAs } from './Page.js'; interface CommandArgs { @@ -165,26 +161,21 @@ class SpoPageAddCommand extends SpoCommand { const template = await request.post<{ UniqueId: string }>(requestOptions); itemId = template.UniqueId; - const listItemId = await this.getFileListItemId(args.options.webUrl, serverRelativeFileUrl); + const listItemId = await this.getFileListItemId(args.options.webUrl, serverRelativeFileUrl, logger); const layoutType: string = args.options.layoutType || 'Article'; - const listItemSetOptions: spoListItemSetOptions = { - webUrl: args.options.webUrl, - listUrl: listServerRelativeUrl, - id: listItemId, - systemUpdate: true, + const listItemSetOptions: any = { ContentTypeId: '0x0101009D1CB255DA76424F860D91F20E6C4118', Title: pageTitle, ClientSideApplicationId: 'b6917cb1-93a0-4b97-a84d-7cf49975d4ec', - PageLayoutType: layoutType, - verbose: this.verbose, - debug: this.debug + PageLayoutType: layoutType }; + if (args.options.layoutType === 'Article') { listItemSetOptions.PromotedState = 0; listItemSetOptions.BannerImageUrl = `${resource}/_layouts/15/images/sitepagethumbnail.png, /_layouts/15/images/sitepagethumbnail.png`; } - await Cli.executeCommand(spoListItemSetCommand as Command, { options: { ...listItemSetOptions, _: [] } }); + await spo.setListItem(args.options.webUrl, listServerRelativeUrl, listItemId, true, listItemSetOptions, logger, this.verbose); const pageProps = await Page.checkout(pageName, args.options.webUrl, logger, this.debug, this.verbose); if (pageProps) { @@ -216,17 +207,11 @@ class SpoPageAddCommand extends SpoCommand { await request.post(requestOptions); break; case 'NewsPage': - const listItemSetOptions: spoListItemSetOptions = { - webUrl: args.options.webUrl, - listUrl: listServerRelativeUrl, - id: listItemId, - systemUpdate: true, + const listItemSetOptions: any = { PromotedState: 2, - FirstPublishedDate: new Date().toISOString(), - verbose: this.verbose, - debug: this.debug + FirstPublishedDate: new Date().toISOString() }; - await Cli.executeCommand(spoListItemSetCommand as Command, { options: { ...listItemSetOptions, _: [] } }); + await spo.setListItem(args.options.webUrl, listServerRelativeUrl, listItemId, true, listItemSetOptions, logger, this.verbose); break; case 'Template': requestOptions.url = `${args.options.webUrl}/_api/SitePages/Pages(${listItemId})/SavePageAsTemplate`; @@ -349,17 +334,10 @@ class SpoPageAddCommand extends SpoCommand { this.handleRejectedODataJsonPromise(err); } } - private async getFileListItemId(webUrl: string, serverRelativeFileUrl: string): Promise { - const fileGetOptions: spoFileGetOptions = { - webUrl: webUrl, - url: serverRelativeFileUrl, - asListItem: true, - verbose: this.verbose, - debug: this.debug - }; - const fileGetOutput: CommandOutput = await Cli.executeCommandWithOutput(spoFileGetCommand as Command, { options: { ...fileGetOptions, _: [] } }); - const fileGetOutputJson = JSON.parse(fileGetOutput.stdout); - return fileGetOutputJson.Id; + + private async getFileListItemId(webUrl: string, serverRelativeFileUrl: string, logger: Logger): Promise { + const file = await spo.getFileAsListItemByUrl(webUrl, serverRelativeFileUrl, logger, this.verbose); + return file.Id; } }