Skip to content
Open
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
144 changes: 144 additions & 0 deletions __tests__/article-image-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const { structuredDataTestHtml } = require('../index')
const presets = require('../presets')

describe('Article Image URL Validation (Issue #22)', () => {
test('should fail when Article image is an ImageObject without url', async () => {
const html = `
<!DOCTYPE html>
<html>
<head>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "Test Article",
"author": {
"@type": "Person",
"name": "John Doe"
},
"datePublished": "2023-01-01",
"image": {
"@type": "ImageObject"
},
"publisher": {
"@type": "Organization",
"name": "Test Publisher",
"logo": {
"@type": "ImageObject",
"url": "https://example.com/logo.jpg"
}
}
}
</script>
</head>
<body><h1>Test</h1></body>
</html>
`

const result = await structuredDataTestHtml(html, { presets: [presets.Google] })
.then(response => response)
.catch(err => err.res || err)

// Should have failures
expect(result.failed.length).toBeGreaterThan(0)

// Should specifically fail on image.url
const imageUrlFailure = result.failed.find(test =>
test.test.includes('image.url')
)
expect(imageUrlFailure).toBeDefined()
expect(imageUrlFailure.groups).toContain('Article')
})

test('should pass when Article image is an ImageObject with url', async () => {
const html = `
<!DOCTYPE html>
<html>
<head>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "Test Article",
"author": {
"@type": "Person",
"name": "John Doe"
},
"datePublished": "2023-01-01",
"dateModified": "2023-01-02",
"mainEntityOfPage": "https://example.com/article",
"image": {
"@type": "ImageObject",
"url": "https://example.com/image.jpg"
},
"publisher": {
"@type": "Organization",
"name": "Test Publisher",
"logo": {
"@type": "ImageObject",
"url": "https://example.com/logo.jpg"
}
}
}
</script>
</head>
<body><h1>Test</h1></body>
</html>
`

const result = await structuredDataTestHtml(html, { presets: [presets.Google] })
.then(response => response)
.catch(err => err.res || err)

// Should have the image.url test pass
const imageUrlTest = result.passed.find(test =>
test.test.includes('image.url')
)
expect(imageUrlTest).toBeDefined()
})

test('should fail when Article publisher.logo is an ImageObject without url', async () => {
const html = `
<!DOCTYPE html>
<html>
<head>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "Test Article",
"author": {
"@type": "Person",
"name": "John Doe"
},
"datePublished": "2023-01-01",
"image": {
"@type": "ImageObject",
"url": "https://example.com/image.jpg"
},
"publisher": {
"@type": "Organization",
"name": "Test Publisher",
"logo": {
"@type": "ImageObject"
}
}
}
</script>
</head>
<body><h1>Test</h1></body>
</html>
`

const result = await structuredDataTestHtml(html, { presets: [presets.Google] })
.then(response => response)
.catch(err => err.res || err)

// Should specifically fail on publisher.logo.url
const logoUrlFailure = result.failed.find(test =>
test.test.includes('publisher.logo.url')
)
expect(logoUrlFailure).toBeDefined()
expect(logoUrlFailure.groups).toContain('Article')
})
})
17 changes: 13 additions & 4 deletions __tests__/fixtures/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ <h1 itemprop="headline">Example Headline in Microdata</h1>
<p>
Published On: <span itemprop="datePublished">2019-01-01</span>
<p>
<img itemprop="image" src="http://example.com/path-to-article-image.jpg" alt="Image description"/>
<div itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
<meta itemprop="url" content="http://example.com/path-to-article-image.jpg"/>
</div>
<p>
Article text…
</p>
Expand All @@ -41,7 +43,9 @@ <h1 itemprop="headline">Second Headline in Microdata</h1>
<p>
Published On: <span itemprop="datePublished">2019-02-01</span>
<p>
<img itemprop="image" src="http://example.com/path-to-article-2-image.jpg" alt="Image description"/>
<div itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
<meta itemprop="url" content="http://example.com/path-to-article-2-image.jpg"/>
</div>
<p>
Article text…
</p>
Expand Down Expand Up @@ -74,7 +78,9 @@ <h1 property="headline">Example Headline in RDFa</h1>
<p>
Published On: <span property="datePublished">2019-01-01</span>
<p>
<img property="image" src="http://example.com/path-to-article-image.jpg" alt="Image description"/>
<div property="image" vocab="http://schema.org/" typeof="ImageObject">
<meta property="url" content="http://example.com/path-to-article-image.jpg"/>
</div>
<p>
Article text…
</p>
Expand All @@ -98,7 +104,10 @@ <h1 property="headline">Example Headline in RDFa</h1>
"headline": "Example Headline in JSON-LD",
"author": "C Smith",
"url": "http://example.com/path-to-article",
"image": "http://example.com/path-to-article-image.jpg",
"image": {
"@type": "ImageObject",
"url": "http://example.com/path-to-article-image.jpg"
},
"datePublished": "2019-01-01",
"publisher": {
"@type": "Organization",
Expand Down
Loading