partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

AbstractTest.js (4135B)


      1 /**
      2  * Represents a test.
      3  *
      4  * Calls a specific PHP file on the server via AJAX and interprets the response.
      5  */
      6 Ext.define('PartKeeprSetup.AbstractTest', {
      7     mixins: {
      8         observable: 'Ext.util.Observable'
      9     },
     10 
     11     /**
     12      * Defines the URL to call
     13      */
     14     url: '../setup.php/setup/',
     15 
     16     action: '',
     17 
     18     /**
     19      * Defines if the call was successful or not.
     20      */
     21     success: false,
     22 
     23     /**
     24      * Defines the callback. This needs to be an object which implements the "appendTestResult" method.
     25      */
     26     callback: null,
     27 
     28     /**
     29      * Defines the name of this test.
     30      */
     31     name: null,
     32 
     33     resultMessage: "",
     34 
     35     message: "",
     36     errors: [],
     37     warnings: [],
     38 
     39     method: 'POST',
     40 
     41     /**
     42      * Defines if a test should be skipped. No output will be generated.
     43      * @var {Boolean}
     44      */
     45     skip: false,
     46 
     47     /**
     48      * Defines additional parameters which are to be sent with the request. The format is an object,
     49      * e.g.
     50      * {
     51 	 *    username: "foo",
     52 	 *    password: "bar"
     53 	 * }
     54      *
     55      *
     56      */
     57     params: {},
     58 
     59     /**
     60      * Constructs the test
     61      */
     62     constructor: function (config)
     63     {
     64         this.mixins.observable.constructor.call(this, config);
     65     },
     66     /**
     67      * Runs a given test, and processes the response
     68      */
     69     run: function ()
     70     {
     71         this.onBeforeRunTest();
     72 
     73         this.callback.outputTestMessage(this);
     74 
     75         if (this.skip === true) {
     76             this.fireEvent("complete", this);
     77             this.success = true;
     78             this.resultMessage = "Skipped";
     79             this.callback.appendTestResult(this);
     80             return;
     81         }
     82         var url = this.url;
     83 
     84         if (this.action !== "") {
     85             url = url + this.action;
     86         }
     87 
     88 
     89         Ext.Ajax.request({
     90             url: url,
     91             success: this.onSuccess,
     92             failure: this.onFailure,
     93             method: this.method,
     94             scope: this,
     95             params: Ext.encode(this.params),
     96             timeout: 120000
     97         });
     98     },
     99 
    100     onFailure: function (response)
    101     {
    102         this.success = false;
    103         this.resultMessage = "Invalid Response from server";
    104         this.errors = ["Invalid Response from server", response.responseText];
    105 
    106         if (this.callback) {
    107             this.callback.appendTestResult(this);
    108         }
    109 
    110         if (this.success) {
    111             this.fireEvent("complete", this);
    112         }
    113 
    114     },
    115     /**
    116      * Callback for the Ext.Ajax.request method. Decodes the response, sets the object parameters, fires the "complete"
    117      * event and calls back the test result panel.
    118      *
    119      * @param response
    120      */
    121     onSuccess: function (response)
    122     {
    123         var obj;
    124 
    125         try {
    126             obj = Ext.decode(response.responseText);
    127         } catch (exception) {
    128             obj = {};
    129             obj.success = false;
    130             obj.message = "Invalid Response from server";
    131             obj.errors = ["Invalid Response from server", response.responseText];
    132         }
    133 
    134         if (!obj.success || obj.success === false) {
    135             if (Ext.isArray(obj.errors)) {
    136                 this.errors = obj.errors;
    137             } else {
    138                 this.errors = [];
    139             }
    140 
    141             this.success = false;
    142         } else {
    143             this.success = true;
    144         }
    145 
    146         if (obj.warnings !== undefined && Ext.isArray(obj.warnings)) {
    147             this.warnings = obj.warnings;
    148         }
    149 
    150         this.resultMessage = obj.message;
    151 
    152         if (this.callback) {
    153             this.callback.appendTestResult(this);
    154         }
    155 
    156         if (this.success) {
    157             this.fireEvent("complete", this);
    158         }
    159 
    160         this.onAfterRunTest(obj);
    161     },
    162     /**
    163      * Gets called prior test execution. Most tests won't use this, but some tests need to inject parameters.
    164      */
    165     onBeforeRunTest: function ()
    166     {
    167         this.params = PartKeeprSetup.getApplication().getSetupConfig();
    168     },
    169     /**
    170      * Gets called after test exeuction. Useful for post-processing any result
    171      *
    172      * @param data {Object} The response data as object
    173      */
    174     onAfterRunTest: function (data)
    175     {
    176 
    177     }
    178 
    179 });