|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { expect, mergeTests } from '@playwright/test' |
| 7 | +import { test as formTest } from '../support/fixtures/form.ts' |
| 8 | +import { test as appNavigationTest } from '../support/fixtures/navigation.ts' |
| 9 | +import { test as randomUserTest } from '../support/fixtures/random-user.ts' |
| 10 | +import { test as submitTest } from '../support/fixtures/submit.ts' |
| 11 | +import { test as topBarTest } from '../support/fixtures/topBar.ts' |
| 12 | +import { createAudio, createMedia, sharePublicly } from '../support/media.ts' |
| 13 | +import { QuestionType } from '../support/sections/QuestionType.ts' |
| 14 | +import { FormsView } from '../support/sections/TopBarSection.ts' |
| 15 | + |
| 16 | +const test = mergeTests( |
| 17 | + randomUserTest, |
| 18 | + appNavigationTest, |
| 19 | + formTest, |
| 20 | + topBarTest, |
| 21 | + submitTest, |
| 22 | +) |
| 23 | + |
| 24 | +const ADD_MEDIA = 'Add image, video or audio' |
| 25 | +const ADDRESS = 'Address of an image, video or audio' |
| 26 | + |
| 27 | +test.describe('Media on questions', () => { |
| 28 | + test.beforeEach(async ({ page, appNavigation, form }) => { |
| 29 | + await page.goto('apps/forms') |
| 30 | + await page.waitForURL(/apps\/forms\/?$/) |
| 31 | + await appNavigation.clickNewForm() |
| 32 | + await form.fillTitle('Question media test form') |
| 33 | + }) |
| 34 | + |
| 35 | + test('Shows an image below the description of a question and in the summary', async ({ |
| 36 | + page, |
| 37 | + form, |
| 38 | + topBar, |
| 39 | + submitView, |
| 40 | + }) => { |
| 41 | + const link = await sharePublicly( |
| 42 | + page, |
| 43 | + 'scene.png', |
| 44 | + await createMedia(page, 'image'), |
| 45 | + ) |
| 46 | + await form.addQuestion(QuestionType.ShortAnswer) |
| 47 | + const [question] = await form.getQuestions() |
| 48 | + await question.fillTitle('What do you see?') |
| 49 | + await question.fillDescription('Look at the picture first.') |
| 50 | + await expect( |
| 51 | + question.section.getByRole('textbox', { name: ADDRESS }), |
| 52 | + ).toHaveCount(0) |
| 53 | + |
| 54 | + await question.openActionsMenu() |
| 55 | + await page.getByRole('button', { name: ADD_MEDIA }).click() |
| 56 | + const address = question.section.getByRole('textbox', { name: ADDRESS }) |
| 57 | + await expect(address).toBeFocused() |
| 58 | + await question.fillMediaField(ADDRESS, link) |
| 59 | + await question.fillMediaField( |
| 60 | + 'Description for screen readers', |
| 61 | + 'A blue square', |
| 62 | + ) |
| 63 | + await expect( |
| 64 | + question.section.getByRole('img', { name: 'A blue square' }), |
| 65 | + ).toBeVisible() |
| 66 | + await question.openActionsMenu() |
| 67 | + await expect(page.getByRole('button', { name: ADD_MEDIA })).toHaveCount(0) |
| 68 | + await page.keyboard.press('Escape') |
| 69 | + |
| 70 | + await topBar.toggleView(FormsView.View) |
| 71 | + const shown = submitView.getQuestion('What do you see?') |
| 72 | + const image = shown.getByRole('img', { name: 'A blue square' }) |
| 73 | + await expect(image).toHaveAttribute( |
| 74 | + 'src', |
| 75 | + /\/apps\/files_sharing\/publicpreview\/[A-Za-z0-9-]+\?x=1280&y=800&a=1$/, |
| 76 | + ) |
| 77 | + expect( |
| 78 | + await shown.evaluate((item) => { |
| 79 | + const [description, picture, input] = [ |
| 80 | + '.question__header__description__output', |
| 81 | + 'img', |
| 82 | + 'input', |
| 83 | + ].map((selector) => item.querySelector(selector)!) |
| 84 | + return ( |
| 85 | + !!( |
| 86 | + description.compareDocumentPosition(picture) |
| 87 | + & Node.DOCUMENT_POSITION_FOLLOWING |
| 88 | + ) |
| 89 | + && !!( |
| 90 | + picture.compareDocumentPosition(input) |
| 91 | + & Node.DOCUMENT_POSITION_FOLLOWING |
| 92 | + ) |
| 93 | + ) |
| 94 | + }), |
| 95 | + ).toBe(true) |
| 96 | + await expect(shown.getByRole('textbox')).toHaveAccessibleDescription( |
| 97 | + /^Look at the picture first\.\s*A blue square$/, |
| 98 | + ) |
| 99 | + await submitView.fillText('What do you see?', 'A square') |
| 100 | + await submitView.submit() |
| 101 | + await expect(submitView.successMessage).toBeVisible() |
| 102 | + |
| 103 | + await topBar.toggleView(FormsView.Results) |
| 104 | + await expect(page.getByRole('img', { name: 'A blue square' })).toBeVisible() |
| 105 | + }) |
| 106 | + |
| 107 | + test('Plays audio attached to a choice question', async ({ |
| 108 | + page, |
| 109 | + form, |
| 110 | + topBar, |
| 111 | + submitView, |
| 112 | + }) => { |
| 113 | + const link = await sharePublicly(page, 'note.wav', createAudio()) |
| 114 | + await form.addQuestion(QuestionType.Checkboxes) |
| 115 | + const [question] = await form.getQuestions() |
| 116 | + await question.fillTitle('Which instrument is it?') |
| 117 | + await question.addAnswer('Piano') |
| 118 | + await question.openActionsMenu() |
| 119 | + await page.getByRole('button', { name: ADD_MEDIA }).click() |
| 120 | + await question.fillMediaField(ADDRESS, link) |
| 121 | + await question.fillMediaField( |
| 122 | + 'Description for screen readers', |
| 123 | + 'Download the note', |
| 124 | + ) |
| 125 | + await expect( |
| 126 | + question.section.getByText( |
| 127 | + 'Add a transcript of the audio to the description', |
| 128 | + ), |
| 129 | + ).toBeVisible() |
| 130 | + |
| 131 | + await topBar.toggleView(FormsView.View) |
| 132 | + const shown = submitView.getQuestion('Which instrument is it?') |
| 133 | + await expect(shown.locator('audio')).toHaveAccessibleName( |
| 134 | + 'Download the note', |
| 135 | + ) |
| 136 | + await expect(shown.getByRole('checkbox', { name: 'Piano' })).toBeVisible() |
| 137 | + }) |
| 138 | + |
| 139 | + test('Removes the image, video or audio item of a question', async ({ |
| 140 | + page, |
| 141 | + form, |
| 142 | + }) => { |
| 143 | + await form.addQuestion(QuestionType.ShortAnswer) |
| 144 | + const [question] = await form.getQuestions() |
| 145 | + await question.fillTitle('Where is it?') |
| 146 | + await question.openActionsMenu() |
| 147 | + await page.getByRole('button', { name: ADD_MEDIA }).click() |
| 148 | + await question.fillMediaField(ADDRESS, 'https://example.com/map') |
| 149 | + |
| 150 | + await question.openActionsMenu() |
| 151 | + const saved = page.waitForResponse( |
| 152 | + (response) => |
| 153 | + response.request().method() === 'PATCH' |
| 154 | + && response.url().includes('/questions/'), |
| 155 | + ) |
| 156 | + await page |
| 157 | + .getByRole('button', { name: 'Remove image, video or audio' }) |
| 158 | + .click() |
| 159 | + expect((await saved).request().postDataJSON()).toEqual({ |
| 160 | + keyValuePairs: { extraSettings: { media: null } }, |
| 161 | + }) |
| 162 | + await expect( |
| 163 | + question.section.getByRole('textbox', { name: ADDRESS }), |
| 164 | + ).toHaveCount(0) |
| 165 | + await expect( |
| 166 | + question.section.getByRole('button', { name: 'Actions', exact: true }), |
| 167 | + ).toBeFocused() |
| 168 | + |
| 169 | + await page.reload() |
| 170 | + const reloaded = page.getByRole('listitem', { name: 'Question number 1' }) |
| 171 | + await expect( |
| 172 | + reloaded.getByRole('textbox', { name: 'Title of question number 1' }), |
| 173 | + ).toHaveValue('Where is it?') |
| 174 | + await expect(reloaded.getByRole('textbox', { name: ADDRESS })).toHaveCount(0) |
| 175 | + await reloaded.getByRole('button', { name: 'Actions', exact: true }).click() |
| 176 | + await expect(page.getByRole('button', { name: ADD_MEDIA })).toBeVisible() |
| 177 | + }) |
| 178 | + |
| 179 | + test('Offers no extra media action on a content block', async ({ |
| 180 | + page, |
| 181 | + form, |
| 182 | + }) => { |
| 183 | + await form.addQuestion(QuestionType.Content) |
| 184 | + const [block] = await form.getQuestions() |
| 185 | + await expect( |
| 186 | + block.section.getByRole('textbox', { name: ADDRESS }), |
| 187 | + ).toBeVisible() |
| 188 | + await block.openActionsMenu() |
| 189 | + await expect(page.getByRole('button', { name: ADD_MEDIA })).toHaveCount(0) |
| 190 | + }) |
| 191 | +}) |
0 commit comments