-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from apostrophecms/pro-4691-redirect-to-other-…
…locales Pro 4691 redirect to other locales
- Loading branch information
Showing
9 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# This is a basic workflow to help you get started with Actions | ||
|
||
name: tests | ||
|
||
# Controls when the action will run. | ||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ '*' ] | ||
|
||
# Allows you to run this workflow manually from the Actions tab | ||
workflow_dispatch: | ||
|
||
# A workflow run is made up of one or more jobs that can run sequentially or in parallel | ||
jobs: | ||
# This workflow contains a single job called "build" | ||
build: | ||
# The type of runner that the job will run on | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: [18, 20] | ||
mongodb-version: ['4.4', '5.0', '6.0'] | ||
|
||
# Steps represent a sequence of tasks that will be executed as part of the job | ||
steps: | ||
- name: Git checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: Start MongoDB | ||
uses: supercharge/mongodb-github-action@1.8.0 | ||
with: | ||
mongodb-version: ${{ matrix.mongodb-version }} | ||
|
||
- run: npm install | ||
|
||
- run: npm test | ||
env: | ||
CI: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
const assert = require('assert').strict; | ||
const t = require('apostrophe/test-lib/util.js'); | ||
|
||
describe('@apostrophecms/redirect', function () { | ||
let apos; | ||
let redirectModule; | ||
|
||
this.timeout(t.timeout); | ||
|
||
after(async function() { | ||
await t.destroy(apos); | ||
}); | ||
|
||
before(async function() { | ||
apos = await t.create({ | ||
root: module, | ||
testModule: true, | ||
modules: getAppConfig() | ||
}); | ||
|
||
redirectModule = apos.modules['@apostrophecms/redirect']; | ||
await insertPages(apos); | ||
}); | ||
|
||
this.afterEach(async function() { | ||
await apos.doc.db.deleteMany({ type: '@apostrophecms/redirect' }); | ||
}); | ||
|
||
it('should allow to redirect to external URLs', async function() { | ||
const req = apos.task.getReq(); | ||
const instance = redirectModule.newInstance(); | ||
await redirectModule.insert(req, { | ||
...instance, | ||
title: 'external redirect', | ||
urlType: 'external', | ||
redirectSlug: '/page-1', | ||
externalUrl: 'http://localhost:3000/page-2' | ||
}); | ||
const redirected = await apos.http.get('http://localhost:3000/page-1'); | ||
|
||
assert.equal(redirected, '<title>page 2</title>\n'); | ||
}); | ||
|
||
it('should allow to redirect to internal pages', async function() { | ||
const req = apos.task.getReq(); | ||
const instance = redirectModule.newInstance(); | ||
const page2 = await apos.page.find(req, { title: 'page 2' }).toObject(); | ||
await redirectModule.insert(req, { | ||
...instance, | ||
title: 'internal redirect', | ||
urlType: 'internal', | ||
redirectSlug: '/page-1', | ||
_newPage: [ page2 ] | ||
}); | ||
const redirected = await apos.http.get('http://localhost:3000/page-1'); | ||
|
||
assert.equal(redirected, '<title>page 2</title>\n'); | ||
}); | ||
|
||
it('should allow to redirect to internal pages in other locales', async function() { | ||
const req = apos.task.getReq(); | ||
const reqFr = apos.task.getReq({ locale: 'fr' }); | ||
const instance = redirectModule.newInstance(); | ||
const pageFr = await apos.page.find(reqFr, { title: 'page fr' }).toObject(); | ||
await redirectModule.insert(req, { | ||
...instance, | ||
title: 'internal redirect', | ||
urlType: 'internal', | ||
redirectSlug: '/page-1', | ||
_newPage: [ pageFr ] | ||
}); | ||
|
||
const redirected = await apos.http.get('http://localhost:3000/page-1'); | ||
assert.equal(redirected, '<title>page fr</title>\n'); | ||
}); | ||
}); | ||
|
||
async function insertPages(apos) { | ||
const req = apos.task.getReq(); | ||
const frReq = apos.task.getReq({ locale: 'fr' }); | ||
const defaultPageModule = apos.modules['default-page']; | ||
const pageInstance = defaultPageModule.newInstance(); | ||
|
||
await apos.page.insert(req, '_home', 'lastChild', { | ||
...pageInstance, | ||
title: 'page 1', | ||
slug: '/page-1' | ||
}); | ||
await apos.page.insert(req, '_home', 'lastChild', { | ||
...pageInstance, | ||
title: 'page 2', | ||
slug: '/page-2' | ||
}); | ||
await apos.page.insert(frReq, '_home', 'lastChild', { | ||
...pageInstance, | ||
title: 'page fr', | ||
slug: '/page-fr' | ||
}); | ||
} | ||
|
||
function getAppConfig() { | ||
return { | ||
'@apostrophecms/express': { | ||
options: { | ||
session: { secret: 'supersecret' }, | ||
port: 3000 | ||
} | ||
}, | ||
'@apostrophecms/i18n': { | ||
options: { | ||
defaultLocale: 'en', | ||
locales: { | ||
en: { label: 'English' }, | ||
fr: { | ||
label: 'French', | ||
prefix: '/fr' | ||
} | ||
} | ||
} | ||
}, | ||
'@apostrophecms/redirect': {}, | ||
'default-page': {}, | ||
article: { | ||
extend: '@apostrophecms/piece-type', | ||
options: { | ||
alias: 'article' | ||
} | ||
}, | ||
topic: { | ||
extend: '@apostrophecms/piece-type', | ||
options: { | ||
alias: 'topic' | ||
}, | ||
fields: { | ||
add: { | ||
description: { | ||
label: 'Description', | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
extend: '@apostrophecms/page-type' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<title>{{ data.page.title }}</title> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"/**": "This package.json file is not actually installed.", | ||
" * ": "Apostrophe requires that all npm modules to be loaded by moog", | ||
" */": "exist in package.json at project level, which for a test is here", | ||
"dependencies": { | ||
"apostrophe": "git+https://github.com/apostrophecms/apostrophe.git", | ||
"@apostrophecms/redirect": "git+https://github.com/apostrophecms/redirect.git" | ||
} | ||
} |