ALF-9157 Add title/name rules for wikis to the wiki service, with tests

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29583 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-05 18:31:17 +00:00
parent 818d49ab8d
commit 4a8f8b7c2f
2 changed files with 63 additions and 9 deletions

View File

@@ -127,6 +127,13 @@ public class WikiServiceImpl implements WikiService
siteService, transactionService, taggingService); siteService, transactionService, taggingService);
} }
private String buildName(String title)
{
// The name is based on the title, but with underscores
String name = title.replace(' ', '_');
return name;
}
private WikiPageInfo buildPage(NodeRef nodeRef, NodeRef container, String name, String preLoadedContents) private WikiPageInfo buildPage(NodeRef nodeRef, NodeRef container, String name, String preLoadedContents)
{ {
WikiPageInfoImpl page = new WikiPageInfoImpl(nodeRef, container, name); WikiPageInfoImpl page = new WikiPageInfoImpl(nodeRef, container, name);
@@ -188,16 +195,19 @@ public class WikiServiceImpl implements WikiService
// Grab the location to store in // Grab the location to store in
NodeRef container = getSiteWikiContainer(siteShortName, true); NodeRef container = getSiteWikiContainer(siteShortName, true);
// Build the name
String name = buildName(title);
// Get the properties for the node // Get the properties for the node
Map<QName, Serializable> props = new HashMap<QName, Serializable>(); Map<QName, Serializable> props = new HashMap<QName, Serializable>();
props.put(ContentModel.PROP_NAME, title); props.put(ContentModel.PROP_NAME, name);
props.put(ContentModel.PROP_TITLE, title); props.put(ContentModel.PROP_TITLE, title);
// Build the node // Build the node
NodeRef nodeRef = nodeService.createNode( NodeRef nodeRef = nodeService.createNode(
container, container,
ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS,
QName.createQName(title), QName.createQName(name),
ContentModel.TYPE_CONTENT, ContentModel.TYPE_CONTENT,
props props
).getChildRef(); ).getChildRef();
@@ -209,7 +219,7 @@ public class WikiServiceImpl implements WikiService
// Generate the wrapping object for it // Generate the wrapping object for it
// Build it that way, so creator and created date come through // Build it that way, so creator and created date come through
return buildPage(nodeRef, container, title, content); return buildPage(nodeRef, container, name, content);
} }
@Override @Override
@@ -222,21 +232,22 @@ public class WikiServiceImpl implements WikiService
} }
NodeRef nodeRef = page.getNodeRef(); NodeRef nodeRef = page.getNodeRef();
String nodeName = buildName(page.getTitle());
// Handle the rename case // Handle the rename case
boolean renamed = false; boolean renamed = false;
if(! nodeService.getProperty(nodeRef, ContentModel.PROP_NAME).equals(page.getTitle())) if(! nodeService.getProperty(nodeRef, ContentModel.PROP_TITLE).equals(page.getTitle()))
{ {
try try
{ {
fileFolderService.rename(nodeRef, page.getTitle()); fileFolderService.rename(nodeRef, nodeName);
renamed = true; renamed = true;
} }
catch(FileNotFoundException e) catch(FileNotFoundException e)
{ {
throw new AlfrescoRuntimeException("Invalid node state - wiki page no longer found"); throw new AlfrescoRuntimeException("Invalid node state - wiki page no longer found");
} }
nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, page.getTitle()); nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, nodeName);
nodeService.setProperty(nodeRef, ContentModel.PROP_TITLE, page.getTitle()); nodeService.setProperty(nodeRef, ContentModel.PROP_TITLE, page.getTitle());
} }
@@ -251,7 +262,7 @@ public class WikiServiceImpl implements WikiService
// If we re-named, re-create the object // If we re-named, re-create the object
if(renamed) if(renamed)
{ {
page = buildPage(nodeRef, page.getContainerNodeRef(), page.getTitle(), page.getContents()); page = buildPage(nodeRef, page.getContainerNodeRef(), nodeName, page.getContents());
} }
// All done // All done

View File

@@ -186,6 +186,7 @@ public class WikiServiceImplTest
page = WIKI_SERVICE.createWikiPage( page = WIKI_SERVICE.createWikiPage(
WIKI_SITE.getShortName(), "Title", "This Is Some Content" WIKI_SITE.getShortName(), "Title", "This Is Some Content"
); );
testNodesToTidy.add(page.getNodeRef());
// Check it // Check it
@@ -201,11 +202,13 @@ public class WikiServiceImplTest
page.setContents("This is new content"); page.setContents("This is new content");
page = WIKI_SERVICE.updateWikiPage(page); page = WIKI_SERVICE.updateWikiPage(page);
assertEquals("New_Title", page.getSystemName()); // Name has underscores
assertEquals("New Title", page.getTitle());
// Fetch, and check // Fetch, and check
page = WIKI_SERVICE.getWikiPage(WIKI_SITE.getShortName(), page.getSystemName()); page = WIKI_SERVICE.getWikiPage(WIKI_SITE.getShortName(), page.getSystemName());
assertEquals("New Title", page.getSystemName()); assertEquals("New_Title", page.getSystemName()); // Name has underscores
assertEquals("New Title", page.getTitle()); assertEquals("New Title", page.getTitle());
assertEquals("This is new content", page.getContents()); assertEquals("This is new content", page.getContents());
assertEquals(TEST_USER, page.getCreator()); assertEquals(TEST_USER, page.getCreator());
@@ -217,6 +220,46 @@ public class WikiServiceImplTest
// Check it went // Check it went
assertEquals(null, WIKI_SERVICE.getWikiPage(WIKI_SITE.getShortName(), page.getSystemName())); assertEquals(null, WIKI_SERVICE.getWikiPage(WIKI_SITE.getShortName(), page.getSystemName()));
// Create a new node with spaces in title
page = WIKI_SERVICE.createWikiPage(
WIKI_SITE.getShortName(), "Title Space", "This Is Some Content"
);
testNodesToTidy.add(page.getNodeRef());
// Check it
assertEquals("Title_Space", page.getSystemName());
assertEquals("Title Space", page.getTitle());
assertEquals("This Is Some Content", page.getContents());
assertEquals(TEST_USER, page.getCreator());
assertEquals(0, page.getTags().size());
// Edit it without renaming
page.setContents("Changed contents");
page = WIKI_SERVICE.updateWikiPage(page);
// Check
page = WIKI_SERVICE.getWikiPage(WIKI_SITE.getShortName(), page.getSystemName());
assertEquals("Title_Space", page.getSystemName());
assertEquals("Title Space", page.getTitle());
assertEquals("Changed contents", page.getContents());
assertEquals(TEST_USER, page.getCreator());
assertEquals(0, page.getTags().size());
// Now edit with renaming
page.setTitle("Alternate Title");
page = WIKI_SERVICE.updateWikiPage(page);
// Check
page = WIKI_SERVICE.getWikiPage(WIKI_SITE.getShortName(), page.getSystemName());
assertEquals("Alternate_Title", page.getSystemName());
assertEquals("Alternate Title", page.getTitle());
assertEquals("Changed contents", page.getContents());
assertEquals(TEST_USER, page.getCreator());
assertEquals(0, page.getTags().size());
} }
/** /**
@@ -503,7 +546,7 @@ public class WikiServiceImplTest
* Checks that the correct permission checking occurs on fetching * Checks that the correct permission checking occurs on fetching
* links listings (which go through canned queries) * links listings (which go through canned queries)
*/ */
@Test public void linksListingPermissionsChecking() throws Exception @Test public void pagesListingPermissionsChecking() throws Exception
{ {
PagingRequest paging = new PagingRequest(10); PagingRequest paging = new PagingRequest(10);
PagingResults<WikiPageInfo> results; PagingResults<WikiPageInfo> results;