Skip to content

Commit 3e06a0a

Browse files
committed
fix: updated special chars in site name
1 parent 0449090 commit 3e06a0a

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"prepare": "sf-install",
9494
"test": "wireit",
9595
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
96+
"test:spec": "nyc mocha \"test/**/*.spec.ts\" --slow 4500 --timeout 600000 --parallel",
9697
"test:only": "wireit",
9798
"unlink-lwr": "yarn unlink @lwrjs/api @lwrjs/app-service @lwrjs/asset-registry @lwrjs/asset-transformer @lwrjs/auth-middleware @lwrjs/base-view-provider @lwrjs/base-view-transformer @lwrjs/client-modules @lwrjs/config @lwrjs/core @lwrjs/dev-proxy-server @lwrjs/diagnostics @lwrjs/esbuild @lwrjs/everywhere @lwrjs/fs-asset-provider @lwrjs/fs-watch @lwrjs/html-view-provider @lwrjs/instrumentation @lwrjs/label-module-provider @lwrjs/lambda @lwrjs/legacy-npm-module-provider @lwrjs/loader @lwrjs/lwc-module-provider @lwrjs/lwc-ssr @lwrjs/markdown-view-provider @lwrjs/module-bundler @lwrjs/module-registry @lwrjs/npm-module-provider @lwrjs/nunjucks-view-provider @lwrjs/o11y @lwrjs/resource-registry @lwrjs/router @lwrjs/security @lwrjs/server @lwrjs/shared-utils @lwrjs/static @lwrjs/tools @lwrjs/types @lwrjs/view-registry lwr",
9899
"update-snapshots": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" snapshot:generate",

src/shared/experience/expSite.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ export class ExperienceSite {
179179

180180
public async getRemoteMetadata(): Promise<SiteMetadata | undefined> {
181181
if (this.metadataCache.remoteMetadata) return this.metadataCache.remoteMetadata;
182+
// Check if there are special characters in the site name
183+
const isInvalidSiteName = hasSpacesOrSpecialChars(this.siteName);
184+
// const originalSiteName = this.siteName;
185+
if (isInvalidSiteName) {
186+
const updatedSiteName = replaceSpacesAndSpecialChars(this.siteName);
187+
this.siteName = updatedSiteName;
188+
}
182189
const result = await this.org
183190
.getConnection()
184191
.query<{ Name: string; LastModifiedDate: string }>(
@@ -356,3 +363,27 @@ function getSiteNameFromStaticResource(staticResourceName: string): string {
356363
}
357364
return parts.slice(4).join(' ');
358365
}
366+
367+
export function replaceSpacesAndSpecialChars(value: string): string {
368+
// Replace any special characters with underscore
369+
value = value.replace(/[^a-zA-Z0-9]/g, '_');
370+
371+
// Replace spaces with underscore
372+
value = value.replace(/ /g, '_');
373+
374+
return value;
375+
}
376+
377+
export function hasSpacesOrSpecialChars(value: string): boolean {
378+
// Check for spaces
379+
if (value.includes(' ')) {
380+
return true;
381+
}
382+
383+
// Check for special characters
384+
if (/[^\w]/.test(value)) {
385+
return true;
386+
}
387+
388+
return false;
389+
}

test/spec/expSite.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2023, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { expect } from 'chai';
8+
import { Connection, Org } from '@salesforce/core';
9+
import sinon from 'sinon';
10+
import {
11+
replaceSpacesAndSpecialChars,
12+
hasSpacesOrSpecialChars,
13+
ExperienceSite,
14+
} from '../../src/shared/experience/expSite.js';
15+
16+
describe('replaceSpacesAndSpecialChars', () => {
17+
it('should replace spaces and special characters with underscores', () => {
18+
const input = 'site#name@with-special%chars with spaces';
19+
const expectedOutput = 'site_name_with_special_chars_with_spaces';
20+
const output = replaceSpacesAndSpecialChars(input);
21+
expect(output).to.equal(expectedOutput);
22+
});
23+
});
24+
25+
describe('hasSpacesOrSpecialChars', () => {
26+
it('should return true if the input string has spaces', () => {
27+
const input = 'Hello World';
28+
const output = hasSpacesOrSpecialChars(input);
29+
expect(output).to.be.true;
30+
});
31+
32+
it('should return true if the input string has special characters', () => {
33+
const input = 'Hello, @#';
34+
const output = hasSpacesOrSpecialChars(input);
35+
expect(output).to.be.true;
36+
});
37+
38+
it('should return false if the input string has neither spaces nor special characters', () => {
39+
const input = 'HelloWorld';
40+
const output = hasSpacesOrSpecialChars(input);
41+
expect(output).to.be.false;
42+
});
43+
});
44+
45+
describe('getRemoteMetadata', () => {
46+
it('should return remote metadata when it exists', async () => {
47+
const org = new Org();
48+
const siteName = 'site@with#special-chars';
49+
const experienceSite = new ExperienceSite(org, siteName);
50+
51+
// Create a mock Connection instance using sinon
52+
const mockConnection = sinon.createStubInstance(Connection);
53+
54+
// Configure the mock to return the desired result when calling query
55+
mockConnection.query.resolves({
56+
done: true,
57+
totalSize: 1,
58+
records: [
59+
{
60+
Name: 'MRT_experience_00DSG00000ECBfZ_0DMSG000001CfA6_site_with_special_chars_10-30_12-47',
61+
LastModifiedDate: '2024-11-12',
62+
},
63+
],
64+
});
65+
66+
// Replace the original connection with the mocked connection
67+
org.getConnection = () => mockConnection;
68+
69+
const remoteMetadata = await experienceSite.getRemoteMetadata();
70+
71+
// Check if the called query matches the expected pattern
72+
const calledQuery = mockConnection.query.args[0][0];
73+
const expectedPattern =
74+
/SELECT Name, LastModifiedDate FROM StaticResource WHERE Name LIKE 'MRT_experience_%_site_with_special_chars/;
75+
expect(calledQuery).to.match(expectedPattern);
76+
77+
expect(remoteMetadata).to.deep.equal({
78+
bundleName: 'MRT_experience_00DSG00000ECBfZ_0DMSG000001CfA6_site_with_special_chars_10-30_12-47',
79+
bundleLastModified: '2024-11-12',
80+
});
81+
});
82+
});

0 commit comments

Comments
 (0)