|
| 1 | +package org.grails.web.taglib |
| 2 | + |
| 3 | +import org.grails.core.artefact.UrlMappingsArtefactHandler |
| 4 | + |
| 5 | +class FormTagLibResourceTests extends AbstractGrailsTagTests { |
| 6 | + |
| 7 | + protected void onInit() { |
| 8 | + def mappingClass = gcl.parseClass(''' |
| 9 | +class TestUrlMappings { |
| 10 | + static mappings = { |
| 11 | + "/books"(resources:"book") { |
| 12 | + "/authors"(resources:"author") |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + ''') |
| 17 | + |
| 18 | + grailsApplication.addArtefact(UrlMappingsArtefactHandler.TYPE, mappingClass) |
| 19 | + } |
| 20 | + |
| 21 | + void testResourceSave() { |
| 22 | + def template = '<g:form resource="book" action="save"/>' |
| 23 | + assertOutputEquals('<form action="/books" method="post" ></form>', template) |
| 24 | + } |
| 25 | + |
| 26 | + void testResourceUpdate() { |
| 27 | + def template = '<g:form resource="book" action="update" id="1"/>' |
| 28 | + assertOutputEquals('<form action="/books/1" method="post" ><input type="hidden" name="_method" value="PUT" id="_method" /></form>', template) |
| 29 | + } |
| 30 | + |
| 31 | + void testResourceUpdateIdInParams() { |
| 32 | + def template = '<g:form resource="book" action="update" params="[id:1]"/>' |
| 33 | + assertOutputEquals('<form action="/books/1" method="post" ><input type="hidden" name="_method" value="PUT" id="_method" /></form>', template) |
| 34 | + } |
| 35 | + |
| 36 | + void testResourcePatch() { |
| 37 | + def template = '<g:form resource="book" action="patch" id="1"/>' |
| 38 | + assertOutputEquals('<form action="/books/1" method="post" ><input type="hidden" name="_method" value="PATCH" id="_method" /></form>', template) |
| 39 | + } |
| 40 | + |
| 41 | + void testResourcePatchIdInParams() { |
| 42 | + def template = '<g:form resource="book" action="patch" params="[id:1]"/>' |
| 43 | + assertOutputEquals('<form action="/books/1" method="post" ><input type="hidden" name="_method" value="PATCH" id="_method" /></form>', template) |
| 44 | + } |
| 45 | + |
| 46 | + void testResourceNestedSave() { |
| 47 | + def template = '<g:form resource="book/author" action="save" params="[bookId:1]"/>' |
| 48 | + assertOutputEquals('<form action="/books/1/authors" method="post" ></form>', template) |
| 49 | + } |
| 50 | + |
| 51 | + void testResourceNestedUpdate() { |
| 52 | + // We'd really like to suppoer this format <g:form resource="book/author" action="update" id="2" bookId="1"/> |
| 53 | + // but the form tag limits the set of attributes it hands to the linkGenerator and the dynamic parameters like 'bookId' get filtered out |
| 54 | + // instead we make do with putting bookId in the params attribute |
| 55 | + def template = '<g:form resource="book/author" action="update" id="2" params="[bookId:1]"/>' |
| 56 | + assertOutputEquals('<form action="/books/1/authors/2" method="post" ><input type="hidden" name="_method" value="PUT" id="_method" /></form>', template) |
| 57 | + } |
| 58 | + |
| 59 | + void testResourceNestedUpdateIdInParams() { |
| 60 | + def template = '<g:form resource="book/author" action="update" params="[bookId:1, id:2]"/>' |
| 61 | + assertOutputEquals('<form action="/books/1/authors/2" method="post" ><input type="hidden" name="_method" value="PUT" id="_method" /></form>', template) |
| 62 | + } |
| 63 | + |
| 64 | + void testResourceNestedPatch() { |
| 65 | + def template = '<g:form resource="book/author" action="patch" id="2" params="[bookId:1]"/>' |
| 66 | + assertOutputEquals('<form action="/books/1/authors/2" method="post" ><input type="hidden" name="_method" value="PATCH" id="_method" /></form>', template) |
| 67 | + } |
| 68 | + |
| 69 | + void testResourceNestedPatchIdInParams() { |
| 70 | + def template = '<g:form resource="book/author" action="patch" params="[bookId:1, id:2]"/>' |
| 71 | + assertOutputEquals('<form action="/books/1/authors/2" method="post" ><input type="hidden" name="_method" value="PATCH" id="_method" /></form>', template) |
| 72 | + } |
| 73 | + |
| 74 | + void assertOutputEquals(expected, template, params = [:]) { |
| 75 | + def engine = appCtx.groovyPagesTemplateEngine |
| 76 | + |
| 77 | + assert engine |
| 78 | + def t = engine.createTemplate(template, "test_"+ System.currentTimeMillis()) |
| 79 | + |
| 80 | + def w = t.make(params) |
| 81 | + |
| 82 | + def sw = new StringWriter() |
| 83 | + def out = new PrintWriter(sw) |
| 84 | + webRequest.out = out |
| 85 | + w.writeTo(out) |
| 86 | + |
| 87 | + assertEquals expected, sw.toString() |
| 88 | + } |
| 89 | +} |
0 commit comments