Skip to content
Snippets Groups Projects
Commit 98c5443e authored by Eduard Moraru's avatar Eduard Moraru
Browse files

XWIKI-10998: After a user deletes his avatar, "User Avatar" is not correctly displayed

- The macro is now checking if the attachment file exists
- Added test and updated existing ones
parent 33e45c35
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
import javax.inject.Singleton; import javax.inject.Singleton;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.xwiki.bridge.DocumentAccessBridge; import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.bridge.SkinAccessBridge; import org.xwiki.bridge.SkinAccessBridge;
import org.xwiki.component.annotation.Component; import org.xwiki.component.annotation.Component;
...@@ -102,6 +103,12 @@ public class UserAvatarMacro extends AbstractMacro<UserAvatarMacroParameters> ...@@ -102,6 +103,12 @@ public class UserAvatarMacro extends AbstractMacro<UserAvatarMacroParameters>
@Named("current") @Named("current")
private EntityReferenceValueProvider currentEntityReferenceValueProvider; private EntityReferenceValueProvider currentEntityReferenceValueProvider;
/**
* Logging framework.
*/
@Inject
private Logger logger;
/** /**
* Create and initialize the descriptor of the macro. * Create and initialize the descriptor of the macro.
*/ */
...@@ -134,14 +141,27 @@ public List<Block> execute(UserAvatarMacroParameters parameters, String content, ...@@ -134,14 +141,27 @@ public List<Block> execute(UserAvatarMacroParameters parameters, String content,
+ "] is not registered in this wiki"); + "] is not registered in this wiki");
} }
ResourceReference imageReference; // Initialize with the default avatar.
if (StringUtils.isBlank(fileName)) { ResourceReference imageReference =
imageReference = new ResourceReference(this.skinAccessBridge.getSkinFile("noavatar.png"), ResourceType.URL); new ResourceReference(this.skinAccessBridge.getSkinFile("noavatar.png"), ResourceType.URL);
} else {
// Try to use the configured avatar.
if (!StringUtils.isBlank(fileName)) {
AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference); AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference);
imageReference =
new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference), // Check if the configured avatar file actually exists.
ResourceType.ATTACHMENT); try {
if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) {
// Use it.
imageReference =
new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference),
ResourceType.ATTACHMENT);
}
} catch (Exception e) {
// Log and fallback on default.
logger.warn("Failed to get the avatar for user [{}]. Using default.",
this.compactWikiEntityReferenceSerializer.serialize(userReference));
}
} }
ImageBlock imageBlock = new ImageBlock(imageReference, false); ImageBlock imageBlock = new ImageBlock(imageReference, false);
......
...@@ -67,6 +67,8 @@ public void initialize(ComponentManager componentManager) throws Exception ...@@ -67,6 +67,8 @@ public void initialize(ComponentManager componentManager) throws Exception
final DocumentReference userWithoutAvatarReference = final DocumentReference userWithoutAvatarReference =
new DocumentReference("wiki", "XWiki", "ExistingUserWithoutAvatar"); new DocumentReference("wiki", "XWiki", "ExistingUserWithoutAvatar");
final DocumentReference userNotExistingReference = new DocumentReference("wiki", "XWiki", "UserNotExisting"); final DocumentReference userNotExistingReference = new DocumentReference("wiki", "XWiki", "UserNotExisting");
final DocumentReference userWithNonExistingAvatarFileReference =
new DocumentReference("wiki", "XWiki", "UserWithNonExistingAvatarFile");
final DocumentReference userClassReference = new DocumentReference("wiki", "XWiki", "XWikiUsers"); final DocumentReference userClassReference = new DocumentReference("wiki", "XWiki", "XWikiUsers");
final DocumentAccessBridge mockDocumentAccessBridge = final DocumentAccessBridge mockDocumentAccessBridge =
registerMockComponent(componentManager, mockery, DocumentAccessBridge.class); registerMockComponent(componentManager, mockery, DocumentAccessBridge.class);
...@@ -75,10 +77,19 @@ public void initialize(ComponentManager componentManager) throws Exception ...@@ -75,10 +77,19 @@ public void initialize(ComponentManager componentManager) throws Exception
allowing(mockDocumentAccessBridge).exists(userWithoutAvatarReference); will(returnValue(true)); allowing(mockDocumentAccessBridge).exists(userWithoutAvatarReference); will(returnValue(true));
allowing(mockDocumentAccessBridge).exists(with(any(String.class))); will(returnValue(false)); allowing(mockDocumentAccessBridge).exists(with(any(String.class))); will(returnValue(false));
allowing(mockDocumentAccessBridge).exists(userNotExistingReference); will(returnValue(false)); allowing(mockDocumentAccessBridge).exists(userNotExistingReference); will(returnValue(false));
allowing(mockDocumentAccessBridge).exists(userWithNonExistingAvatarFileReference); will(returnValue(true));
allowing(mockDocumentAccessBridge).getProperty(adminUserReference, userClassReference, "avatar"); allowing(mockDocumentAccessBridge).getProperty(adminUserReference, userClassReference, "avatar");
will(returnValue("mockAvatar.png")); will(returnValue("mockAvatar.png"));
allowing(mockDocumentAccessBridge).getProperty(userWithoutAvatarReference, userClassReference, allowing(mockDocumentAccessBridge).getProperty(userWithoutAvatarReference, userClassReference,
"avatar"); will(returnValue(null)); "avatar"); will(returnValue(null));
allowing(mockDocumentAccessBridge).getProperty(userWithNonExistingAvatarFileReference,
userClassReference, "avatar"); will(returnValue("mockAvatar.png"));
allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png",
adminUserReference)); will(returnValue("1.1"));
allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png",
userWithNonExistingAvatarFileReference)); will(returnValue(null));
}}); }});
// Document Resolver Mock // Document Resolver Mock
...@@ -94,6 +105,9 @@ public void initialize(ComponentManager componentManager) throws Exception ...@@ -94,6 +105,9 @@ public void initialize(ComponentManager componentManager) throws Exception
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserNotExisting", allowing(mockDocumentReferenceResolver).resolve("XWiki.UserNotExisting",
new EntityReference("XWiki", EntityType.SPACE)); new EntityReference("XWiki", EntityType.SPACE));
will(returnValue(userNotExistingReference)); will(returnValue(userNotExistingReference));
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithNonExistingAvatarFile",
new EntityReference("XWiki", EntityType.SPACE));
will(returnValue(userWithNonExistingAvatarFileReference));
}}); }});
// Entity Reference Serializer Mock // Entity Reference Serializer Mock
...@@ -105,6 +119,9 @@ public void initialize(ComponentManager componentManager) throws Exception ...@@ -105,6 +119,9 @@ public void initialize(ComponentManager componentManager) throws Exception
will(returnValue("XWiki.Admin@mockAvatar.png")); will(returnValue("XWiki.Admin@mockAvatar.png"));
allowing(mockEntityReferenceSerializer).serialize(userNotExistingReference); allowing(mockEntityReferenceSerializer).serialize(userNotExistingReference);
will(returnValue("XWiki.UserNotExisting")); will(returnValue("XWiki.UserNotExisting"));
allowing(mockEntityReferenceSerializer).serialize(
new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference));
will(returnValue("XWiki.UserWithNonExistingAvatarFile@mockAvatar.png"));
}}); }});
// Entity Reference Serializer Mock // Entity Reference Serializer Mock
......
.runTransformations
.#--------------------------------------------------------------------
.input|xwiki/2.0
.# Verify that the macro generates an error if the user doesn't exist.
.#--------------------------------------------------------------------
{{useravatar username="XWiki.UserWithNonExistingAvatarFile" /}}
.#--------------------------------------------------------------------
.expect|event/1.0
.#--------------------------------------------------------------------
beginDocument
beginMacroMarkerStandalone [useravatar] [username=XWiki.UserWithNonExistingAvatarFile]
onImage [Typed = [true] Type = [url] Reference = [/xwiki/noavatar.png]] [false] [[alt]=[Picture of UserWithNonExistingAvatarFile][title]=[UserWithNonExistingAvatarFile]]
endMacroMarkerStandalone [useravatar] [username=XWiki.UserWithNonExistingAvatarFile]
endDocument
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment