Skip to content

Commit

Permalink
feat(template): make template handling more generic to support other …
Browse files Browse the repository at this point in the history
…field types

Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Aug 26, 2024
1 parent 52cc761 commit a811e61
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
7 changes: 4 additions & 3 deletions cypress/e2e/templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('Create templates with fields', () => {

cy.login(randUser)
cy.visit('/apps/files')

// Create a templates folder
cy.get('.files-list__header div[menu-title="New"] button')
.should('be.visible')
Expand All @@ -126,8 +126,9 @@ describe('Create templates with fields', () => {

it('Create a document from a template with fields', () => {
const fields = [
{ alias: 'Name', content: 'Nextcloud' },
{ alias: 'Favorite app', content: 'richdocuments' }
{ type: 'rich-text', alias: 'Name', content: 'Nextcloud' },
{ type: 'rich-text', alias: 'Favorite app', content: 'richdocuments' },
{ type: 'checkbox', alias: 'Uses Nextcloud at home', checked: true },
]

cy.visit('/apps/files')
Expand Down
Binary file modified cypress/fixtures/templates/document_template_with_fields.odt
Binary file not shown.
34 changes: 29 additions & 5 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,21 @@ Cypress.Commands.add('submitTemplateFields', (fields) => {
cy.get('.template-field-modal__buttons').as('templateFillerButtons')

for (const field of fields) {
cy.get('@templateFiller')
.find(`input[placeholder="${field.alias}"]`)
.type(field.content)
switch (field.type) {
case 'rich-text':
cy.get('@templateFiller')
.find(`input[placeholder="${field.alias}"]`)
.type(field.content)
break
case 'checkbox':
cy.get('@templateFiller')
.find('span.checkbox-radio-switch__text').contains(field.alias)
.click()
break
default:
expect.fail('Using a field type not yet supported')
break
}
}

// Submit the template fields
Expand All @@ -332,11 +344,23 @@ Cypress.Commands.add('verifyTemplateFields', (fields, fileId) => {
method: 'GET',
url: Cypress.env('baseUrl') + apiEndpoint + fileId + '?format=json',
headers: {
requesttoken
requesttoken,
},
}).then(({ body }) => {
for (const index in body.ocs.data) {
expect(body.ocs.data[index].content).to.equal(fields[index].content)
const field = body.ocs.data[index]

switch (field.type) {
case 'rich-text':
expect(field.content).to.equal(fields[index].content)
break
case 'checkbox':
expect(field.checked).to.equal(fields[index].checked)
break
default:
expect.fail('Using a field type not yet supported')
break
}
}
})
})
Expand Down
28 changes: 18 additions & 10 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Template\Field;
use OCP\Files\Template\FieldFactory;
use OCP\Files\Template\FieldType;
use OCP\Http\Client\IClientService;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -94,16 +95,23 @@ public function extractFields(Node|int $file): array {
continue;
}

$fields[] = [
new Field(
$index,
$attr['content'],
$fieldType,
$attr['alias'],
$attr['id'],
$attr['tag']
)
];
$field = FieldFactory::createField($index, $fieldType);

Check failure on line 98 in lib/Service/TemplateFieldService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedClass

lib/Service/TemplateFieldService.php:98:14: UndefinedClass: Class, interface or enum named OCP\Files\Template\FieldFactory does not exist (see https://psalm.dev/019)
$field->id = $attr['id'];
$field->tag = $attr['tag'];
$field->alias = $attr['alias'];

switch ($fieldType) {
case FieldType::RichText:
$field->setValue($attr['content']);
break;
case FieldType::CheckBox:
$field->setValue($attr['Checked'] === 'true');
break;
default:
break;
}

$fields[] = [$field];
}

$fields = array_merge([], ...$fields);
Expand Down

0 comments on commit a811e61

Please sign in to comment.