Skip to content

Commit

Permalink
feat(gatsby-source-drupal): add support for Drupal JSON API 2.x File …
Browse files Browse the repository at this point in the history
…URI format (#9835)

Description of the issue
#9834

(If this approach is correct, I could add a test for this case)
  • Loading branch information
dcorb authored and pieh committed Nov 13, 2018
1 parent 16e516e commit e86d141
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/gatsby-source-drupal/src/__tests__/fixtures/file.json
Expand Up @@ -9,6 +9,18 @@
"filename": "main-image.png",
"url": "/sites/default/files/main-image.png"
}
},
{
"type": "file--file",
"id": "file-2",
"attributes": {
"id": 2,
"uuid": "file-2",
"filename": "secondary-image.png",
"uri": {
"url" : "/sites/default/files/secondary-image.png"
}
}
}
],
"links": {}
Expand Down
26 changes: 24 additions & 2 deletions packages/gatsby-source-drupal/src/__tests__/index.js
Expand Up @@ -12,11 +12,19 @@ jest.mock(`axios`, () => {
}
})

jest.mock(`gatsby-source-filesystem`, () => {
return {
createRemoteFileNode: jest.fn(),
}
})
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)

const { sourceNodes } = require(`../gatsby-node`)

describe(`gatsby-source-drupal`, () => {
const nodes = {}
const createNodeId = id => `generated-id-${id}`
const baseUrl = `http://fixture`

beforeAll(async () => {
const args = {
Expand All @@ -26,12 +34,13 @@ describe(`gatsby-source-drupal`, () => {
},
}

await sourceNodes(args, { baseUrl: `http://fixture` })
await sourceNodes(args, { baseUrl })
})

it(`Generates nodes`, () => {
expect(Object.keys(nodes).length).not.toEqual(0)
expect(nodes[createNodeId(`file-1`)]).toBeDefined()
expect(nodes[createNodeId(`file-2`)]).toBeDefined()
expect(nodes[createNodeId(`tag-1`)]).toBeDefined()
expect(nodes[createNodeId(`tag-2`)]).toBeDefined()
expect(nodes[createNodeId(`article-1`)]).toBeDefined()
Expand Down Expand Up @@ -97,5 +106,18 @@ describe(`gatsby-source-drupal`, () => {
).toEqual(expect.arrayContaining([createNodeId(`article-1`)]))
})

// TO-DO: add tests for fetching files
it(`Download files`, () => {
const urls = [
`/sites/default/files/main-image.png`,
`/sites/default/files/secondary-image.png`,
].map(fileUrl => new URL(fileUrl, baseUrl).href)

urls.forEach(url => {
expect(createRemoteFileNode).toBeCalledWith(
expect.objectContaining({
url,
})
)
})
})
})
7 changes: 6 additions & 1 deletion packages/gatsby-source-drupal/src/gatsby-node.js
Expand Up @@ -187,8 +187,13 @@ exports.sourceNodes = async (
node.internal.type === `file__file`
) {
try {
let fileUrl = node.url
if (typeof node.uri === `object`) {
// Support JSON API 2.x file URI format https://www.drupal.org/node/2982209
fileUrl = node.uri.url
}
// Resolve w/ baseUrl if node.uri isn't absolute.
const url = new URL(node.url, baseUrl)
const url = new URL(fileUrl, baseUrl)
fileNode = await createRemoteFileNode({
url: url.href,
store,
Expand Down

0 comments on commit e86d141

Please sign in to comment.