|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | + |
| 6 | +from bedrock.mozorg.fixtures.base_fixtures import get_placeholder_image, get_test_index_page |
| 7 | +from bedrock.mozorg.models import HomePage |
| 8 | + |
| 9 | + |
| 10 | +def get_donate_variants(image_id: int) -> list[dict]: |
| 11 | + """Return list of DonateBlock data variants for testing. |
| 12 | +
|
| 13 | + Args: |
| 14 | + image_id: ID of the placeholder image to use |
| 15 | +
|
| 16 | + Returns: |
| 17 | + List of block data dictionaries representing different configurations |
| 18 | + """ |
| 19 | + return [ |
| 20 | + # Variant 1: Default gray background, no anchor |
| 21 | + { |
| 22 | + "type": "donate_block", |
| 23 | + "value": { |
| 24 | + "settings": { |
| 25 | + "background_color": "gray", |
| 26 | + "anchor_id": "", |
| 27 | + }, |
| 28 | + "heading": "Support Mozilla", |
| 29 | + "body": "<p>Help us build a better internet for everyone.</p>", |
| 30 | + "image": image_id, |
| 31 | + "image_alt": "Mozilla community event", |
| 32 | + "cta_text": "Donate Now", |
| 33 | + "cta_link": { |
| 34 | + "link_to": "custom_url", |
| 35 | + "custom_url": "https://foundation.mozilla.org/donate", |
| 36 | + "new_window": False, |
| 37 | + }, |
| 38 | + }, |
| 39 | + "id": "donate-variant-1", |
| 40 | + }, |
| 41 | + # Variant 2: Pink background |
| 42 | + { |
| 43 | + "type": "donate_block", |
| 44 | + "value": { |
| 45 | + "settings": { |
| 46 | + "background_color": "pink", |
| 47 | + "anchor_id": "", |
| 48 | + }, |
| 49 | + "heading": "Join the Movement", |
| 50 | + "body": "<p>Your donation powers our mission.</p><p>Every contribution counts.</p>", |
| 51 | + "image": image_id, |
| 52 | + "image_alt": "", |
| 53 | + "cta_text": "Give Today", |
| 54 | + "cta_link": { |
| 55 | + "link_to": "custom_url", |
| 56 | + "custom_url": "https://foundation.mozilla.org/give", |
| 57 | + "new_window": False, |
| 58 | + }, |
| 59 | + }, |
| 60 | + "id": "donate-variant-2", |
| 61 | + }, |
| 62 | + # Variant 3: With anchor_id |
| 63 | + { |
| 64 | + "type": "donate_block", |
| 65 | + "value": { |
| 66 | + "settings": { |
| 67 | + "background_color": "green", |
| 68 | + "anchor_id": "donate-section", |
| 69 | + }, |
| 70 | + "heading": "Make a Difference", |
| 71 | + "body": "<p>Support open source.</p>", |
| 72 | + "image": image_id, |
| 73 | + "image_alt": "Open source illustration", |
| 74 | + "cta_text": "Contribute", |
| 75 | + "cta_link": { |
| 76 | + "link_to": "custom_url", |
| 77 | + "custom_url": "https://foundation.mozilla.org/contribute", |
| 78 | + "new_window": False, |
| 79 | + }, |
| 80 | + }, |
| 81 | + "id": "donate-variant-3", |
| 82 | + }, |
| 83 | + # Variant 4: With new_window=True |
| 84 | + { |
| 85 | + "type": "donate_block", |
| 86 | + "value": { |
| 87 | + "settings": { |
| 88 | + "background_color": "orange", |
| 89 | + "anchor_id": "", |
| 90 | + }, |
| 91 | + "heading": "Support Our Work", |
| 92 | + "body": "<p>Opens in a <strong>new window</strong>.</p>", |
| 93 | + "image": image_id, |
| 94 | + "image_alt": "", |
| 95 | + "cta_text": "Donate (New Window)", |
| 96 | + "cta_link": { |
| 97 | + "link_to": "custom_url", |
| 98 | + "custom_url": "https://foundation.mozilla.org/external", |
| 99 | + "new_window": True, |
| 100 | + }, |
| 101 | + }, |
| 102 | + "id": "donate-variant-4", |
| 103 | + }, |
| 104 | + ] |
| 105 | + |
| 106 | + |
| 107 | +def get_donate_test_page() -> HomePage: |
| 108 | + """Create a HomePage with all donate block variants for testing. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + HomePage instance with donate blocks populated |
| 112 | + """ |
| 113 | + # Ensure we have a placeholder image |
| 114 | + placeholder_image = get_placeholder_image() |
| 115 | + |
| 116 | + # Get variants with the image ID |
| 117 | + variants = get_donate_variants(placeholder_image.id) |
| 118 | + |
| 119 | + # Get the index page as parent |
| 120 | + index_page = get_test_index_page() |
| 121 | + |
| 122 | + # Check if test page already exists |
| 123 | + test_page = HomePage.objects.filter(slug="donate-block-test").first() |
| 124 | + if test_page: |
| 125 | + return test_page |
| 126 | + |
| 127 | + # Create the test page with all variants |
| 128 | + test_page = HomePage( |
| 129 | + title="Donate Block Test Page", |
| 130 | + slug="donate-block-test", |
| 131 | + donate=variants, |
| 132 | + ) |
| 133 | + |
| 134 | + index_page.add_child(instance=test_page) |
| 135 | + test_page.save_revision().publish() |
| 136 | + |
| 137 | + return test_page |
0 commit comments