Sunday, November 24, 2013

Short detective story (Magento TAF)

Introduction

Hello. I’m pleased, that you, Developer, read this article.
Can you tell me: have you ever thought that many people imagine that a developer, who searches for a bug, looks like a detective that moment?
Ok. You can disagree with me. But when I look for a bug I feel like a real detective from these books or TV series.
So... Sometimes, when long autumn evening comes and rain keeps everybody home,
I take some bug, that isn’t worth fixing during work time, and begin my investigation...



MTAF & Yaml - lost links

Let me present my character. He is a developer. During his practice, as any developer, he spends 30-50% of his time debugging. He likes bugs that force him to think; he gets most inspiration and gratification when he finds answers and elegant solutions.
That evening he decided to find out why a couple of tests for orders from the latest revision of MTAF didn’t pass with Magento 1.8.0.0.
That moment he didn’t know that the trivial lack of knowledge of MTAF uimaps was going to lead him the wrong path. Now it is obvious for him, that, when sfYaml loads file to array, it doesn’t use links and simply copies data where the link should be. But in that rainy evening he was sitting before a display, he was sipping hot coffee and listening to music...


Something goes wrong

There was a failure with the incorrect message about invalid email. He looked at the test.
He was impatient to find something really interesting. So, he immediately figured out what he had to find. It was a ‘not_valid_email’ message within ‘manage_sales_orders’ page. There was this message in the uimap file: fixture/default/core/Mage/Checkout/uimap/admin/AdminCheckout.yml.
And there was exactly what he was looking for: the ‘manage_sales_orders’ page.
He decided to override this file with a 1.8.0 specific one and created: fixture/magento1800/core/Mage/Checkout/uimap/admin/AdminCheckout.yml
---

manage_sales_orders:
 uimap:
  messages: &orderMessages
   not_valid_email: //*[contains(text(),'"Email" is not ...
Then he added magento1800 in the front of the fallbackOrderFixture value.
And... Nothing happened! Really.
It seemed that the rain and wind behind the window were becoming more severe.
He still wanted to solve this easy case as soon as possible, and he made two hypotheses. As every decent detective he should guess and assume.
He thought “There are only two possible causes for such behavior: system can’t load the file for some reason or Mage_Selenium_Helper_Uimap can’t merge files.” But the last one seemed to be a stupid idea.
He has never been an old-school detective with a hat and a notepad. Oh, I mean - the PHP developer who debugs with Notepad and “echo”. He likes IDEs, automated tests and debuggers. It is like long-distance cameras, infrared goggles, chemical test kits and other Batman-like tools (I shouldn’t talk too much about it, may be you know it better than me).
But he just checked if the file was loading with the “echo” in the Mage_Selenium_Helper_File::loadYamlFile method. Everything seemed ok, but the test wasn’t passing.
Then a couple of print_r showed an incredible thing: there were his changes in the configuration at the appropriate place, after everything was initialized and just before assertion was going to pass!
But there was also a little clue, that was noticed. The alias of actual page was different, but the uimap of this page has a link to the place where the message had to be overridden.
In that particular moment our hero had been already suspecting where exactly the answer was. But it was the time of investigation, the code was opened, the instruments were ready. And he decided to sort out how this “m-staff” works...


The answer

He went deep inside uimapping. The first and single suspect was the Mage_Selenium_Helper_Uimap::_mergeUimapIncludes method. He didn’t understand at first glance what this method did and how. Of course, xdebug is appropriate thing to use if you want to understand how exactly something works. Especially if logic seems accurate but some bit doesn’t produce expected results. This research could have been useless if he hadn’t noticed that there was no merge of his data for appropriate page. And as far as we both know it couldn’t be there! But it was a clue, it was the evidence that proves who the murderer is. After sfYaml reads data to array it doesn’t preserve links and the merge method of MTAF can’t even think about links preservation. So, if you want to change values for all pages, that refer to some block of data, you should change it for all these pages (I’m using terms of uimaps).
The bug was fixed.
---
#Manage Sales Orders and related pages

# 'Orders' page
create_order_for_new_customer:
    uimap:
        messages:
            not_valid_email: //*[contains(text(),'"Email" is not a valid email address.') or text()='Please enter a valid email address. For example johndoe@domain.com.']
The rain stopped and starts could be seen again through the clouds.


The end

Thank you if you are reading these lines. It means you have come to the end of my plot. The Special thanks if you aren’t a developer, but didn’t close this article after reading its first lines.
I chose this issue, because it is short and simple, but it still allows to show how incredible it is to be a Magento developer. Because, Magento world gives PHP developers the opportunity to deal non-stop with such things like patterns, automated tests, coding standards, debuggers and so on. And not just because we like such thing, but because this world is so immanence, that they are really worthy of being used.

Photo by LukaTDB

1 comment:

  1. very nice blog to read and to get inform i like it very much and impressed from it you know that you are so beautiful about your work so keep it up


    Magentocommerce - Magento Development

    ReplyDelete