Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wordpress): ensure all file links are rewritten #31652

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
jest.mock(`../dist/utils/fetch-graphql`, () => jest.fn())

import { getHelpers } from "../dist/utils/get-gatsby-api"
import fetchGraphql from "../dist/utils/fetch-graphql"
import { fetchMediaItemsBySourceUrl } from "../dist/steps/source-nodes/fetch-nodes/fetch-referenced-media-items"
import { createContentDigest } from "gatsby-core-utils"
import store from "../dist/store"

const fakeReporter = {
panic: msg => {
console.error(msg)
},
info: msg => {
console.log(msg)
},
}

const createApi = () => {
return {
actions: {
createTypes: jest.fn(),
createNode: jest.fn(),
deleteNode: jest.fn(),
},
reporter: fakeReporter,
createNodeId: jest.fn(),
async getNode(id) {
return {
localFile: {
id: id,
},
}
},
}
}

describe(`fetchMediaItemsBySourceUrl`, () => {
beforeAll(() => {
store.dispatch.gatsbyApi.setState({
pluginOptions: {
schema: {
perPage: 2,
},
},
})
})

afterEach(() => {
jest.resetAllMocks()
})

it(`should properly download multiple pages`, async () => {
fetchGraphql
.mockResolvedValueOnce({
data: {
mediaItem__index_0: null,
mediaItem__index_1: null,
},
})
.mockResolvedValueOnce({
data: {
mediaItem__index_2: {
id: 2,
mediaItemUrl: `https://wordpress.host/wp-content/uploads/2018/05/file1.mp3`,
},
mediaItem__index_3: {
id: 3,
mediaItemUrl: `https://wordpress.host/wp-content/uploads/2018/05/file1.mp3`,
},
},
})
.mockResolvedValueOnce({
data: {
mediaItem__index_4: null,
mediaItem__index_5: null,
},
})
.mockResolvedValueOnce({
data: {
mediaItem__index_6: null,
mediaItem__index_7: null,
},
})
const result = await fetchMediaItemsBySourceUrl({
mediaItemUrls: [
`https://wordpress.host/wp-content/uploads/2018/05/file1.mp3?_=7`,
`https://wordpress.host/wp-content/uploads/2018/05/file2.mp3?_=7`,
`https://wordpress.host/wp-content/uploads/2018/05/file1.mp3`,
`https://wordpress.host/wp-content/uploads/2018/05/file2.mp3`,
],
createContentDigest,
helpers: createApi(),
})
expect(result).toHaveLength(2)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const processAndDedupeImageUrls = urls =>
}, urls)
)

const fetchMediaItemsBySourceUrl = async ({
export const fetchMediaItemsBySourceUrl = async ({
mediaItemUrls,
selectionSet,
builtFragments,
Expand Down Expand Up @@ -319,10 +319,16 @@ const fetchMediaItemsBySourceUrl = async ({
// we pass this resolve function into the queue function so it can let us
// know when it's finished
let resolveFutureNodes
const allResolvedNodes = [...previouslyCachedMediaItemNodes]
let resolveCountTogo = mediaItemUrlsPages.length
const futureNodes = new Promise(resolve => {
resolveFutureNodes = (nodes = []) =>
// combine our resolved nodes we fetched with our cached nodes
resolve([...nodes, ...previouslyCachedMediaItemNodes])
// combine our resolved nodes we fetched with our cached nodes
resolveFutureNodes = (nodes = []) => {
allResolvedNodes.push(...nodes)
if (--resolveCountTogo === 0) {
resolve(allResolvedNodes)
}
}
})

// we have no media items to fetch,
Expand Down