summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2014-03-05 23:46:30 +0000
committerPhilip Withnall <philip@tecnocode.co.uk>2014-05-27 21:54:12 +0100
commit1ec045434e3c0e73d6660ded14374870f20d3e92 (patch)
tree99e475a61bf134b3ecaeb0acde2c73f2fa62a996
parent33b3e5c5bf37c1db55c98b493cb70b40bbf337b7 (diff)
downloadlibgdata-1ec045434e3c0e73d6660ded14374870f20d3e92.tar.gz
core: Load extra attributes from ‘kind’ categories
When a GDataEntry is created, it’s automatically given a ‘kind’ category element, as this is the way the GData protocol identifies the schema which applies to a given <entry>. Previously, the ‘kind’ category from parsed XML would not override the automatically-added category. This meant that if the XML contained extra attributes, such as a label, they were effectively lost. Fix this by always preferring the ‘kind’ category from the XML. Add a test case, because test cases are cool. https://bugzilla.gnome.org/show_bug.cgi?id=707477
-rw-r--r--gdata/gdata-entry.c14
-rw-r--r--gdata/tests/general.c45
2 files changed, 59 insertions, 0 deletions
diff --git a/gdata/gdata-entry.c b/gdata/gdata-entry.c
index b46cc8e3..41b80c88 100644
--- a/gdata/gdata-entry.c
+++ b/gdata/gdata-entry.c
@@ -784,11 +784,25 @@ gdata_entry_add_category (GDataEntry *self, GDataCategory *category)
/* Check to see if it's a kind category and if it matches the entry's predetermined kind */
if (g_strcmp0 (gdata_category_get_scheme (category), "http://schemas.google.com/g/2005#kind") == 0) {
GDataEntryClass *klass = GDATA_ENTRY_GET_CLASS (self);
+ GList *element;
if (klass->kind_term != NULL && g_strcmp0 (gdata_category_get_term (category), klass->kind_term) != 0) {
g_warning ("Adding a kind category term, '%s', to an entry of kind '%s'.",
gdata_category_get_term (category), klass->kind_term);
}
+
+ /* If it is a kind category, remove the entry’s existing kind category to allow the new one
+ * to be added. This is necessary because the existing category was set in
+ * gdata_entry_constructed() and might not contain all the attributes of the actual XML
+ * category.
+ *
+ * See: https://bugzilla.gnome.org/show_bug.cgi?id=707477 */
+ element = g_list_find_custom (self->priv->categories, category, (GCompareFunc) gdata_comparable_compare);
+ if (element != NULL) {
+ g_assert (GDATA_IS_CATEGORY (element->data));
+ g_object_unref (element->data);
+ self->priv->categories = g_list_delete_link (self->priv->categories, element);
+ }
}
/* Add the category if we don't already have it */
diff --git a/gdata/tests/general.c b/gdata/tests/general.c
index 2bb445c0..52f9b32b 100644
--- a/gdata/tests/general.c
+++ b/gdata/tests/general.c
@@ -492,6 +492,50 @@ test_entry_parse_xml (void)
}
static void
+test_entry_parse_xml_kind_category (void)
+{
+ GDataEntry *entry;
+ GError *error = NULL;
+
+ g_test_bug ("707477");
+
+ /* Create an entry from XML with a ‘kind’ category with extra attributes. */
+ entry = GDATA_ENTRY (gdata_parsable_new_from_xml (GDATA_TYPE_ENTRY,
+ "<entry xmlns='http://www.w3.org/2005/Atom'>"
+ "<title type='text'>Testing kind categories</title>"
+ "<updated>2009-01-25T14:07:37Z</updated>"
+ "<published>2009-01-23T14:06:37Z</published>"
+ "<content type='text'>Here we test kind categories.</content>"
+ "<category scheme='http://schemas.google.com/g/2005#kind' "
+ "term='http://schemas.google.com/docs/2007#file' "
+ "label='application/vnd.oasis.opendocument.presentation'/>"
+ "<category scheme='http://schemas.google.com/g/2005/labels' "
+ "term='http://schemas.google.com/g/2005/labels#modified-by-me' "
+ "label='modified-by-me'/>"
+ "</entry>", -1, &error));
+ g_assert_no_error (error);
+ g_assert (GDATA_IS_ENTRY (entry));
+ g_clear_error (&error);
+
+ /* Now check the outputted XML from the entry still has the extra attributes */
+ gdata_test_assert_xml (entry,
+ "<?xml version='1.0' encoding='UTF-8'?>"
+ "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>"
+ "<title type='text'>Testing kind categories</title>"
+ "<updated>2009-01-25T14:07:37Z</updated>"
+ "<published>2009-01-23T14:06:37Z</published>"
+ "<content type='text'>Here we test kind categories.</content>"
+ "<category term='http://schemas.google.com/docs/2007#file' "
+ "scheme='http://schemas.google.com/g/2005#kind' "
+ "label='application/vnd.oasis.opendocument.presentation'/>"
+ "<category term='http://schemas.google.com/g/2005/labels#modified-by-me' "
+ "scheme='http://schemas.google.com/g/2005/labels' "
+ "label='modified-by-me'/>"
+ "</entry>");
+ g_object_unref (entry);
+}
+
+static void
test_entry_error_handling (void)
{
GDataEntry *entry;
@@ -4297,6 +4341,7 @@ main (int argc, char *argv[])
g_test_add_func ("/entry/get_xml", test_entry_get_xml);
g_test_add_func ("/entry/parse_xml", test_entry_parse_xml);
g_test_add_func ("/entry/error_handling", test_entry_error_handling);
+ g_test_add_func ("/entry/parse_xml/kind_category", test_entry_parse_xml_kind_category);
g_test_add_func ("/entry/escaping", test_entry_escaping);
g_test_add_func ("/entry/links/remove", test_entry_links_remove);