Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Update Collection and Category mutations for handling alt texts

fowczarekc06571eb092a
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index f026731ef7..f9e2c7956b 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -25,6 +25,8 @@ class CategoryInput(graphene.InputObjectType):
slug = graphene.String(description='Category slug')
seo = SeoInput(description='Search engine optimization fields.')
background_image = Upload(description='Background image file.')
+ background_image_alt = graphene.String(
+ description='Alt text for an image.')
class CategoryCreate(ModelMutation):
@@ -109,6 +111,8 @@ class CollectionInput(graphene.InputObjectType):
slug = graphene.String(description='Slug of the collection.')
description = graphene.String(description='Description of the collection.')
background_image = Upload(description='Background image file.')
+ background_image_alt = graphene.String(
+ description='Alt text for an image.')
seo = SeoInput(description='Search engine optimization fields.')
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 6a457313ad..32c181aa0d 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -460,7 +460,7 @@ class Category(CountableDjangoObjectType):
be used for navigation in the storefront.""")
exclude_fields = [
'lft', 'rght', 'tree_id', 'voucher_set', 'sale_set',
- 'menuitem_set']
+ 'menuitem_set', 'background_image_alt']
interfaces = [relay.Node]
model = models.Category
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index 6559996b6e..d004b9de1b 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -50,13 +50,14 @@ def test_category_query(user_api_client, product):
def test_category_create_mutation(
monkeypatch, staff_api_client, permission_manage_products):
query = """
- mutation($name: String, $slug: String, $description: String, $backgroundImage: Upload, $parentId: ID) {
+ mutation($name: String, $slug: String, $description: String, $backgroundImage: Upload, $backgroundImageAlt: String, $parentId: ID) {
categoryCreate(
input: {
name: $name
slug: $slug
description: $description
backgroundImage: $backgroundImage
+ backgroundImageAlt: $backgroundImageAlt
},
parent: $parentId
) {
@@ -69,6 +70,9 @@ def test_category_create_mutation(
name
id
}
+ backgroundImage{
+ alt
+ }
}
errors {
field
@@ -88,11 +92,13 @@ def test_category_create_mutation(
category_slug = slugify(category_name)
category_description = 'Test description'
image_file, image_name = create_image()
+ image_alt = 'Alt text for an image.'
# test creating root category
variables = {
'name': category_name, 'description': category_description,
- 'backgroundImage': image_name, 'slug': category_slug}
+ 'backgroundImage': image_name, 'backgroundImageAlt': image_alt,
+ 'slug': category_slug}
body = get_multipart_request_body(query, variables, image_file, image_name)
response = staff_api_client.post_multipart(
body, permissions=[permission_manage_products])
@@ -105,6 +111,7 @@ def test_category_create_mutation(
category = Category.objects.get(name=category_name)
assert category.background_image.file
mock_create_thumbnails.assert_called_once_with(category.pk)
+ assert data['category']['backgroundImage']['alt'] == image_alt
# test creating subcategory
parent_id = data['category']['id']
@@ -161,13 +168,14 @@ def test_category_create_mutation_without_background_image(
def test_category_update_mutation(
monkeypatch, staff_api_client, category, permission_manage_products):
query = """
- mutation($id: ID!, $name: String, $slug: String, $backgroundImage: Upload, $description: String) {
+ mutation($id: ID!, $name: String, $slug: String, $backgroundImage: Upload, $backgroundImageAlt: String, $description: String) {
categoryUpdate(
id: $id
input: {
name: $name
description: $description
backgroundImage: $backgroundImage
+ backgroundImageAlt: $backgroundImageAlt
slug: $slug
}
) {
@@ -178,6 +186,9 @@ def test_category_update_mutation(
parent {
id
}
+ backgroundImage{
+ alt
+ }
}
errors {
field
@@ -201,12 +212,13 @@ def test_category_update_mutation(
category_slug = slugify(category_name)
category_description = 'Updated description'
image_file, image_name = create_image()
+ image_alt = 'Alt text for an image.'
category_id = graphene.Node.to_global_id('Category', child_category.pk)
variables = {
'name': category_name, 'description': category_description,
- 'backgroundImage': image_name, 'id': category_id,
- 'slug': category_slug}
+ 'backgroundImage': image_name, 'backgroundImageAlt': image_alt,
+ 'id': category_id, 'slug': category_slug}
body = get_multipart_request_body(query, variables, image_file, image_name)
response = staff_api_client.post_multipart(
body, permissions=[permission_manage_products])
@@ -222,6 +234,7 @@ def test_category_update_mutation(
category = Category.objects.get(name=category_name)
assert category.background_image.file
mock_create_thumbnails.assert_called_once_with(category.pk)
+ assert data['category']['backgroundImage']['alt'] == image_alt
def test_category_update_mutation_without_background_image(
diff --git a/tests/api/test_collection.py b/tests/api/test_collection.py
index 9644149b29..e3a3398cd5 100644
--- a/tests/api/test_collection.py
+++ b/tests/api/test_collection.py
@@ -56,9 +56,9 @@ def test_create_collection(
monkeypatch, staff_api_client, product_list, permission_manage_products):
query = """
mutation createCollection(
- $name: String!, $slug: String!, $description: String, $products: [ID], $backgroundImage: Upload!, $isPublished: Boolean!) {
+ $name: String!, $slug: String!, $description: String, $products: [ID], $backgroundImage: Upload!, $backgroundImageAlt: String, $isPublished: Boolean!) {
collectionCreate(
- input: {name: $name, slug: $slug, description: $description, products: $products, backgroundImage: $backgroundImage, isPublished: $isPublished}) {
+ input: {name: $name, slug: $slug, description: $description, products: $products, backgroundImage: $backgroundImage, backgroundImageAlt: $backgroundImageAlt, isPublished: $isPublished}) {
collection {
name
slug
@@ -66,6 +66,9 @@ def test_create_collection(
products {
totalCount
}
+ backgroundImage{
+ alt
+ }
}
}
}
@@ -80,13 +83,14 @@ def test_create_collection(
product_ids = [
to_global_id('Product', product.pk) for product in product_list]
image_file, image_name = create_image()
+ image_alt = 'Alt text for an image.'
name = 'test-name'
slug = 'test-slug'
description = 'test-description'
variables = {
'name': name, 'slug': slug, 'description': description,
'products': product_ids, 'backgroundImage': image_name,
- 'isPublished': True}
+ 'backgroundImageAlt': image_alt, 'isPublished': True}
body = get_multipart_request_body(query, variables, image_file, image_name)
response = staff_api_client.post_multipart(
body, permissions=[permission_manage_products])
@@ -99,6 +103,7 @@ def test_create_collection(
collection = Collection.objects.get(slug=slug)
assert collection.background_image.file
mock_create_thumbnails.assert_called_once_with(collection.pk)
+ assert data['backgroundImage']['alt'] == image_alt
def test_create_collection_without_background_image(
@@ -171,17 +176,21 @@ def test_update_collection_with_background_image(
monkeypatch, staff_api_client, collection, permission_manage_products):
query = """
mutation updateCollection(
- $name: String!, $slug: String!, $id: ID!, $backgroundImage: Upload, $isPublished: Boolean!) {
+ $name: String!, $slug: String!, $id: ID!, $backgroundImage: Upload, $backgroundImageAlt: String, $isPublished: Boolean!) {
collectionUpdate(
id: $id, input: {
- name: $name,
+ name: $name,
slug: $slug,
- backgroundImage: $backgroundImage,
+ backgroundImage: $backgroundImage,
+ backgroundImageAlt: $backgroundImageAlt,
isPublished: $isPublished
}
) {
collection {
slug
+ backgroundImage{
+ alt
+ }
}
errors {
field
@@ -198,11 +207,13 @@ def test_update_collection_with_background_image(
mock_create_thumbnails)
image_file, image_name = create_image()
+ image_alt = 'Alt text for an image.'
variables = {
'name': 'new-name',
'slug': 'new-slug',
'id': to_global_id('Collection', collection.id),
'backgroundImage': image_name,
+ 'backgroundImageAlt': image_alt,
'isPublished': True}
body = get_multipart_request_body(query, variables, image_file, image_name)
response = staff_api_client.post_multipart(
@@ -214,6 +225,7 @@ def test_update_collection_with_background_image(
collection = Collection.objects.get(slug=slug)
assert collection.background_image
mock_create_thumbnails.assert_called_once_with(collection.pk)
+ assert data['collection']['backgroundImage']['alt'] == image_alt
def test_delete_collection(