| 1 |
|
| 2 |
|
| 3 |
from odoo.fields import Command |
| 4 |
from odoo.tests import tagged |
| 5 |
|
| 6 |
from odoo.addons.base.tests.common import HttpCaseWithUserDemo |
| 7 |
from odoo.addons.product.tests.common import ProductVariantsCommon |
| 8 |
from odoo.addons.sale.tests.common import SaleCommon |
| 9 |
|
| 10 |
|
| 11 |
@tagged('post_install', '-at_install') |
| 12 |
class TestProductConfiguratorData(HttpCaseWithUserDemo, ProductVariantsCommon, SaleCommon): |
| 13 |
|
| 14 |
def request_get_values(self, product_template, ptav_ids=None): |
| 15 |
base_url = product_template.get_base_url() |
| 16 |
response = self.url_open( |
| 17 |
url=base_url + '/sale/product_configurator/get_values', |
| 18 |
json={ |
| 19 |
'params': { |
| 20 |
'product_template_id': product_template.id, |
| 21 |
'quantity': 1.0, |
| 22 |
'currency_id': 1, |
| 23 |
'so_date': str(self.env.cr.now()), |
| 24 |
'product_uom_id': None, |
| 25 |
'company_id': None, |
| 26 |
'pricelist_id': None, |
| 27 |
'ptav_ids': ptav_ids, |
| 28 |
'only_main_product': False, |
| 29 |
}, |
| 30 |
} |
| 31 |
) |
| 32 |
return response.json()['result'] |
| 33 |
|
| 34 |
def create_product_template_with_2_attributes(self): |
| 35 |
return self.env['product.template'].create({ |
| 36 |
'name': 'Shirt', |
| 37 |
'categ_id': self.product_category.id, |
| 38 |
'attribute_line_ids': [ |
| 39 |
Command.create({ |
| 40 |
'attribute_id': self.size_attribute.id, |
| 41 |
'value_ids': [ |
| 42 |
Command.set([ |
| 43 |
self.size_attribute_l.id, |
| 44 |
self.size_attribute_m.id, |
| 45 |
]), |
| 46 |
], |
| 47 |
}), |
| 48 |
Command.create({ |
| 49 |
'attribute_id': self.color_attribute.id, |
| 50 |
'value_ids': [ |
| 51 |
Command.set([ |
| 52 |
self.color_attribute_red.id, |
| 53 |
self.color_attribute_blue.id, |
| 54 |
]) |
| 55 |
], |
| 56 |
}), |
| 57 |
], |
| 58 |
}) |
| 59 |
|
| 60 |
def create_product_template_with_attribute_no_variant(self): |
| 61 |
return self.env['product.template'].create({ |
| 62 |
'name': 'Chair', |
| 63 |
'categ_id': self.product_category.id, |
| 64 |
'attribute_line_ids': [ |
| 65 |
Command.create({ |
| 66 |
'attribute_id': self.no_variant_attribute.id, |
| 67 |
'value_ids': [ |
| 68 |
Command.set([ |
| 69 |
self.no_variant_attribute_extra.id |
| 70 |
]), |
| 71 |
], |
| 72 |
}), |
| 73 |
Command.create({ |
| 74 |
'attribute_id': self.color_attribute.id, |
| 75 |
'value_ids': [ |
| 76 |
Command.set([ |
| 77 |
self.color_attribute_red.id, |
| 78 |
self.color_attribute_blue.id, |
| 79 |
]) |
| 80 |
], |
| 81 |
}), |
| 82 |
], |
| 83 |
}) |
| 84 |
|
| 85 |
def create_product_template_with_2_attribute_no_variant(self): |
| 86 |
return self.env['product.template'].create({ |
| 87 |
'name': 'Chair', |
| 88 |
'categ_id': self.product_category.id, |
| 89 |
'attribute_line_ids': [ |
| 90 |
Command.create({ |
| 91 |
'attribute_id': self.no_variant_attribute.id, |
| 92 |
'value_ids': [ |
| 93 |
Command.set([ |
| 94 |
self.no_variant_attribute_extra.id, |
| 95 |
self.no_variant_attribute_second.id, |
| 96 |
]), |
| 97 |
], |
| 98 |
}), |
| 99 |
Command.create({ |
| 100 |
'attribute_id': self.color_attribute.id, |
| 101 |
'value_ids': [ |
| 102 |
Command.set([ |
| 103 |
self.color_attribute_red.id, |
| 104 |
self.color_attribute_blue.id, |
| 105 |
]) |
| 106 |
], |
| 107 |
}), |
| 108 |
], |
| 109 |
}) |
| 110 |
|
| 111 |
def test_dropped_value_isnt_shown(self): |
| 112 |
self.assertEqual(len(self.product_template_sofa.product_variant_ids), 3) |
| 113 |
|
| 114 |
|
| 115 |
self.empty_order.order_line = [ |
| 116 |
Command.create({ |
| 117 |
'product_id': product.id |
| 118 |
}) |
| 119 |
for product in self.product_template_sofa.product_variant_ids |
| 120 |
] |
| 121 |
self.empty_order.action_confirm() |
| 122 |
|
| 123 |
|
| 124 |
self.product_template_sofa.attribute_line_ids.value_ids -= self.color_attribute_red |
| 125 |
self.assertEqual(len(self.product_template_sofa.product_variant_ids.filtered('active')), 2) |
| 126 |
|
| 127 |
self.authenticate('demo', 'demo') |
| 128 |
result = self.request_get_values(self.product_template_sofa) |
| 129 |
|
| 130 |
|
| 131 |
self.assertEqual(len(result['products'][0]['attribute_lines'][0]['attribute_values']), 2) |
| 132 |
|
| 133 |
def test_dropped_attribute(self): |
| 134 |
product_template = self.create_product_template_with_2_attributes() |
| 135 |
self.assertEqual(len(product_template.product_variant_ids), 4) |
| 136 |
|
| 137 |
|
| 138 |
self.empty_order.order_line = [ |
| 139 |
Command.create({ |
| 140 |
'product_id': product.id |
| 141 |
}) |
| 142 |
for product in product_template.product_variant_ids |
| 143 |
] |
| 144 |
self.empty_order.action_confirm() |
| 145 |
|
| 146 |
|
| 147 |
product_template.attribute_line_ids[0].unlink() |
| 148 |
self.assertEqual(len(product_template.product_variant_ids), 2) |
| 149 |
|
| 150 |
self.authenticate('demo', 'demo') |
| 151 |
result = self.request_get_values(product_template) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
self.assertFalse(result['products'][0]['archived_combinations']) |
| 156 |
|
| 157 |
def test_dropped_attribute_value(self): |
| 158 |
product_template = self.create_product_template_with_2_attributes() |
| 159 |
self.assertEqual(len(product_template.product_variant_ids), 4) |
| 160 |
|
| 161 |
|
| 162 |
self.empty_order.order_line = [ |
| 163 |
Command.create( |
| 164 |
{ |
| 165 |
'product_id': product.id |
| 166 |
} |
| 167 |
) |
| 168 |
for product in product_template.product_variant_ids |
| 169 |
] |
| 170 |
self.empty_order.action_confirm() |
| 171 |
|
| 172 |
|
| 173 |
product_template.attribute_line_ids.filtered( |
| 174 |
lambda ptal: ptal.attribute_id == self.color_attribute |
| 175 |
).value_ids = [Command.unlink(self.color_attribute_red.id)] |
| 176 |
self.assertEqual(len(product_template.product_variant_ids), 2) |
| 177 |
archived_variants = product_template.with_context( |
| 178 |
active_test=False |
| 179 |
).product_variant_ids - product_template.product_variant_ids |
| 180 |
self.assertEqual(len(archived_variants), 2) |
| 181 |
|
| 182 |
archived_ptav = product_template.attribute_line_ids.product_template_value_ids.filtered( |
| 183 |
lambda ptav: ptav.product_attribute_value_id == self.color_attribute_red |
| 184 |
) |
| 185 |
|
| 186 |
variant_ptav_ids = [ |
| 187 |
archived_ptav.id, |
| 188 |
product_template.attribute_line_ids.product_template_value_ids.filtered( |
| 189 |
lambda ptav: ptav.product_attribute_value_id == self.size_attribute_l |
| 190 |
).id, |
| 191 |
] |
| 192 |
self.authenticate('demo', 'demo') |
| 193 |
result = self.request_get_values(product_template, variant_ptav_ids) |
| 194 |
archived_ptav = archived_variants.product_template_attribute_value_ids.filtered( |
| 195 |
lambda ptav: ptav.product_attribute_value_id == self.color_attribute_red |
| 196 |
) |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
self.assertEqual( |
| 201 |
len(result['products'][0]['archived_combinations']), |
| 202 |
2 |
| 203 |
) |
| 204 |
for combination in result['products'][0]['archived_combinations']: |
| 205 |
self.assertIn(archived_ptav.id, combination) |
| 206 |
|
| 207 |
|
| 208 |
self.assertIn(str(archived_ptav.id), result['products'][0]['exclusions']) |
| 209 |
|
| 210 |
def test_excluded_inactive_ptav(self): |
| 211 |
product_template = self.create_product_template_with_2_attributes() |
| 212 |
self.assertEqual(len(product_template.product_variant_ids), 4) |
| 213 |
|
| 214 |
ptav_with_exclusion = product_template.attribute_line_ids[0].product_template_value_ids[0] |
| 215 |
ptav_excluded = product_template.attribute_line_ids[1].product_template_value_ids[0] |
| 216 |
|
| 217 |
|
| 218 |
ptav_with_exclusion.write({ |
| 219 |
'exclude_for': [ |
| 220 |
Command.create({ |
| 221 |
'product_tmpl_id': product_template.id, |
| 222 |
'value_ids': [ |
| 223 |
Command.set([ |
| 224 |
ptav_excluded.id, |
| 225 |
]), |
| 226 |
], |
| 227 |
}), |
| 228 |
], |
| 229 |
}) |
| 230 |
self.assertEqual(len(product_template.product_variant_ids), 3) |
| 231 |
|
| 232 |
self.authenticate('demo', 'demo') |
| 233 |
result = self.request_get_values(product_template) |
| 234 |
|
| 235 |
self.assertEqual(result['products'][0]['exclusions'] |
| 236 |
[str(ptav_with_exclusion.id)], [ptav_excluded.id]) |
| 237 |
self.assertEqual(result['products'][0]['exclusions'] |
| 238 |
[str(ptav_excluded.id)], [ptav_with_exclusion.id]) |
| 239 |
|
| 240 |
ptav_with_exclusion.write({'ptav_active': False}) |
| 241 |
result = self.request_get_values(product_template) |
| 242 |
|
| 243 |
self.assertFalse(str(ptav_with_exclusion.id) in result['products'][0]['exclusions']) |
| 244 |
|
| 245 |
self.assertEqual(result['products'][0]['exclusions'][str(ptav_excluded.id)], []) |
| 246 |
|
| 247 |
ptav_with_exclusion.write({'ptav_active': True}) |
| 248 |
ptav_excluded.write({'ptav_active': False}) |
| 249 |
result = self.request_get_values(product_template) |
| 250 |
|
| 251 |
self.assertEqual(result['products'][0]['exclusions'][str(ptav_with_exclusion.id)], []) |
| 252 |
|
| 253 |
self.assertFalse(str(ptav_excluded.id) in result['products'][0]['exclusions']) |
| 254 |
|
| 255 |
ptav_with_exclusion.write({'ptav_active': False}) |
| 256 |
ptav_excluded.write({'ptav_active': False}) |
| 257 |
result = self.request_get_values(product_template) |
| 258 |
|
| 259 |
|
| 260 |
self.assertFalse(str(ptav_with_exclusion.id) in result['products'][0]['exclusions']) |
| 261 |
self.assertFalse(str(ptav_excluded.id) in result['products'][0]['exclusions']) |
| 262 |
|
| 263 |
def test_ptal_values_set_for_no_variant_atribute(self): |
| 264 |
''' |
| 265 |
Test that selected_attribute_value_id is set for attribute with only one variant and |
| 266 |
`create_variant`: `no_variant`. |
| 267 |
''' |
| 268 |
product_template = self.create_product_template_with_attribute_no_variant() |
| 269 |
|
| 270 |
self.authenticate('demo', 'demo') |
| 271 |
|
| 272 |
ptav_red = product_template.attribute_line_ids.product_template_value_ids.filtered( |
| 273 |
lambda ptav: ptav.product_attribute_value_id == self.color_attribute_red |
| 274 |
) |
| 275 |
result = self.request_get_values(product_template, [ptav_red.id]) |
| 276 |
self.assertTrue(result['products'][0]['attribute_lines'][1]['selected_attribute_value_ids']) |
| 277 |
|
| 278 |
def test_dropped_attribute_value_custom_no_variant(self): |
| 279 |
product_template = self.create_product_template_with_2_attribute_no_variant() |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
self.empty_order.order_line = [ |
| 284 |
Command.create({ |
| 285 |
'product_id': product.id, |
| 286 |
'product_no_variant_attribute_value_ids': product.attribute_line_ids.product_template_value_ids.filtered( |
| 287 |
lambda p: p.attribute_id.create_variant == 'no_variant' |
| 288 |
), |
| 289 |
}) |
| 290 |
for product in product_template.product_variant_ids] |
| 291 |
self.empty_order.action_confirm() |
| 292 |
|
| 293 |
|
| 294 |
product_template.attribute_line_ids.filtered( |
| 295 |
lambda ptal: ptal.attribute_id == self.no_variant_attribute |
| 296 |
).value_ids = [Command.unlink(self.no_variant_attribute_extra.id)] |
| 297 |
|
| 298 |
archived_ptav = product_template.attribute_line_ids.product_template_value_ids.filtered( |
| 299 |
lambda ptav: ptav.product_attribute_value_id == self.no_variant_attribute_extra |
| 300 |
) |
| 301 |
self.assertFalse(archived_ptav.ptav_active) |
| 302 |
self.assertEqual( |
| 303 |
product_template.attribute_line_ids.filtered( |
| 304 |
lambda ptal: ptal.attribute_id == self.no_variant_attribute |
| 305 |
).product_template_value_ids[0], |
| 306 |
archived_ptav, |
| 307 |
) |
| 308 |
|
| 309 |
variant_ptav_ids = [ |
| 310 |
product_template.attribute_line_ids.product_template_value_ids.filtered( |
| 311 |
lambda ptav: ptav.product_attribute_value_id == self.color_attribute_red |
| 312 |
).id, |
| 313 |
] |
| 314 |
self.authenticate('demo', 'demo') |
| 315 |
result = self.request_get_values(product_template, variant_ptav_ids) |
| 316 |
selected_values = [ |
| 317 |
selected_value |
| 318 |
for product in result['products'][0]['attribute_lines'] |
| 319 |
for selected_value in product['selected_attribute_value_ids']] |
| 320 |
|
| 321 |
|
| 322 |
self.assertNotIn(archived_ptav.id, selected_values) |
| 323 |
|
| 324 |
def test_attribute_removal(self): |
| 325 |
product_template_2lines_2attributes = self.env['product.template'].create({ |
| 326 |
'name': '2 lines 2 attributes', |
| 327 |
'uom_id': self.uom_unit.id, |
| 328 |
'categ_id': self.product_category.id, |
| 329 |
'attribute_line_ids': [ |
| 330 |
Command.create({ |
| 331 |
'attribute_id': self.color_attribute.id, |
| 332 |
'value_ids': [Command.set([ |
| 333 |
self.color_attribute_red.id, |
| 334 |
self.color_attribute_blue.id, |
| 335 |
])], |
| 336 |
}), |
| 337 |
Command.create({ |
| 338 |
'attribute_id': self.size_attribute.id, |
| 339 |
'value_ids': [Command.set([ |
| 340 |
self.size_attribute_s.id, |
| 341 |
self.size_attribute_m.id, |
| 342 |
])] |
| 343 |
}) |
| 344 |
] |
| 345 |
}) |
| 346 |
|
| 347 |
|
| 348 |
self.empty_order.order_line = [ |
| 349 |
Command.create({ |
| 350 |
'product_id': product.id, |
| 351 |
}) |
| 352 |
for product in product_template_2lines_2attributes.product_variant_ids |
| 353 |
] |
| 354 |
|
| 355 |
def _get_ptavs(): |
| 356 |
return product_template_2lines_2attributes.with_context( |
| 357 |
active_test=False |
| 358 |
).attribute_line_ids.product_template_value_ids |
| 359 |
|
| 360 |
def _get_archived_variants(): |
| 361 |
return product_template_2lines_2attributes.with_context( |
| 362 |
active_test=False |
| 363 |
).product_variant_ids.filtered(lambda p: not p.active) |
| 364 |
|
| 365 |
def _get_active_variants(): |
| 366 |
return product_template_2lines_2attributes.product_variant_ids |
| 367 |
|
| 368 |
self.assertEqual(len(_get_ptavs()), 4) |
| 369 |
product_template_2lines_2attributes.attribute_line_ids = [ |
| 370 |
Command.unlink(product_template_2lines_2attributes.attribute_line_ids.filtered( |
| 371 |
lambda ptal: ptal.attribute_id.id == self.size_attribute.id |
| 372 |
).id) |
| 373 |
] |
| 374 |
self.assertEqual(len(_get_ptavs()), 4) |
| 375 |
|
| 376 |
|
| 377 |
self.empty_order.order_line = [ |
| 378 |
Command.create({ |
| 379 |
'product_id': product.id, |
| 380 |
}) |
| 381 |
for product in product_template_2lines_2attributes.product_variant_ids |
| 382 |
] |
| 383 |
|
| 384 |
self.assertEqual(len(_get_archived_variants()), 4) |
| 385 |
self.assertEqual(len(_get_active_variants()), 2) |
| 386 |
|
| 387 |
product_template_2lines_2attributes.attribute_line_ids = [ |
| 388 |
Command.create({ |
| 389 |
'attribute_id': self.size_attribute.id, |
| 390 |
'value_ids': [Command.set([ |
| 391 |
self.size_attribute_s.id, |
| 392 |
])] |
| 393 |
}) |
| 394 |
] |
| 395 |
self.assertEqual(len(_get_ptavs()), 4) |
| 396 |
self.assertEqual(len(_get_active_variants()), 2) |
| 397 |
self.assertEqual(len(_get_archived_variants()), 4) |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
exclusions_data = product_template_2lines_2attributes._get_attribute_exclusions() |
| 405 |
self.assertTrue( |
| 406 |
all( |
| 407 |
tuple(product.product_template_attribute_value_ids.ids) not in exclusions_data['archived_combinations'] |
| 408 |
for product in _get_active_variants() |
| 409 |
) |
| 410 |
) |
| 411 |
|
| 412 |
def test_multiple_attribute_lines_same_attribute(self): |
| 413 |
""" |
| 414 |
Test that product configurator works correctly when multiple attribute |
| 415 |
lines reference the same attribute. This ensures no KeyError is raised |
| 416 |
when building the attrs_map in _get_product_information. |
| 417 |
""" |
| 418 |
|
| 419 |
product_template = self.env['product.template'].create({ |
| 420 |
'name': 'Multi Size Shirt', |
| 421 |
'categ_id': self.product_category.id, |
| 422 |
'attribute_line_ids': [ |
| 423 |
Command.create({ |
| 424 |
'attribute_id': self.size_attribute.id, |
| 425 |
'value_ids': [ |
| 426 |
Command.set([ |
| 427 |
self.size_attribute_l.id, |
| 428 |
]), |
| 429 |
], |
| 430 |
}), |
| 431 |
Command.create({ |
| 432 |
'attribute_id': self.color_attribute.id, |
| 433 |
'value_ids': [ |
| 434 |
Command.set([ |
| 435 |
self.color_attribute_red.id, |
| 436 |
self.color_attribute_blue.id, |
| 437 |
]) |
| 438 |
], |
| 439 |
}), |
| 440 |
Command.create({ |
| 441 |
'attribute_id': self.size_attribute.id, |
| 442 |
'value_ids': [ |
| 443 |
Command.set([ |
| 444 |
self.size_attribute_m.id, |
| 445 |
]), |
| 446 |
], |
| 447 |
}), |
| 448 |
], |
| 449 |
}) |
| 450 |
|
| 451 |
self.authenticate('demo', 'demo') |
| 452 |
|
| 453 |
result = self.request_get_values(product_template) |
| 454 |
|
| 455 |
|
| 456 |
self.assertEqual(len(result['products'][0]['attribute_lines']), 3) |
| 457 |
|
| 458 |
attribute_names = [ |
| 459 |
line['attribute']['name'] |
| 460 |
for line in result['products'][0]['attribute_lines'] |
| 461 |
] |
| 462 |
self.assertIn('Size', attribute_names) |
| 463 |
self.assertIn('Color', attribute_names) |
| 464 |
|
| 465 |
self.assertEqual(attribute_names.count('Size'), 2) |